chatServices.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. angular.module('chatServices', [])
  2. .factory('Connection', function($rootScope, $timeout, $interval, $location) {
  3. var WS = window['MozWebSocket'] ? MozWebSocket : WebSocket;
  4. var chatSocket = null;
  5. var ping = null;
  6. var service = {
  7. username: '',
  8. error: null,
  9. messages: [],
  10. status: 'disconnected',
  11. hasRoom: function() {
  12. return $location.path() != '';
  13. },
  14. isConnected: function() {
  15. return this.status == 'connected';
  16. },
  17. addListener: function(f) {
  18. messageListeners.add(f);
  19. }
  20. };
  21. function wrap(func) {
  22. return function() {
  23. var args = arguments;
  24. $timeout(function() {
  25. func.apply(null, args);
  26. });
  27. }
  28. };
  29. function makeid() {
  30. var text = "";
  31. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  32. for( var i=0; i < 5; i++ )
  33. text += possible.charAt(Math.floor(Math.random() * possible.length));
  34. return text;
  35. }
  36. function getRoom() {
  37. if(!$location.path()) {
  38. $location.path(makeid());
  39. }
  40. return $location.path();
  41. }
  42. service.connect = function(username) {
  43. if(service.status != 'disconnected') return;
  44. service.error = null;
  45. service.status = 'connecting';
  46. chatSocket = new WS(jsRoutes.controllers.Application.chat(username, getRoom()).webSocketURL());
  47. chatSocket.onmessage = wrap(function(event) {
  48. var message = JSON.parse(event.data);
  49. if(message.error) {
  50. service.error = message.error;
  51. }
  52. if(message.kind != "pong") {
  53. $rootScope.$broadcast('ws:message', message);
  54. }
  55. });
  56. chatSocket.onopen = wrap(function() {
  57. $rootScope.$broadcast('ws:connected', username);
  58. service.status = 'connected';
  59. service.username = username;
  60. ping = $interval(function() {
  61. service.send('/ping');
  62. }, 60000, 0, false);
  63. });
  64. chatSocket.onclose = wrap(function() {
  65. $rootScope.$broadcast('ws:disconnected');
  66. service.status = 'disconnected';
  67. service.username = '';
  68. $interval.cancel(ping);
  69. });
  70. }
  71. service.disconnect = function() {
  72. chatSocket.close();
  73. chatSocket = null;
  74. }
  75. service.send = function(message) {
  76. chatSocket.send(JSON.stringify({text: message}));
  77. }
  78. return service;
  79. })
  80. .factory('Chat', function($rootScope, Connection) {
  81. var service = {
  82. username: '',
  83. messages: [],
  84. receive: function(message) {
  85. if(arguments.length == 3) {
  86. message = {
  87. kind: arguments[0],
  88. user: arguments[1],
  89. message: arguments[2]
  90. };
  91. }
  92. service.messages.push(message);
  93. },
  94. send: Connection.send
  95. };
  96. $rootScope.$on('ws:connected', function(event, username) {
  97. service.username = username;
  98. });
  99. $rootScope.$on('ws:message', function(event, message) {
  100. if(message.kind == "talk") {
  101. service.receive(message);
  102. }
  103. else if(message.kind == "join") {
  104. service.receive("join", message.user, " has joined.");
  105. }
  106. else if(message.kind == "quit") {
  107. service.receive("quit", message.user, " has left.");
  108. }
  109. });
  110. $rootScope.$on('ws:disconnected', function() {
  111. service.messages = [];
  112. service.username = '';
  113. });
  114. if(Connection.isConnected()) {
  115. service.username = Connection.username;
  116. }
  117. return service;
  118. })