1
0

chatServices.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. angular.module('chatServices', [])
  2. .factory('Connection', function($rootScope, $timeout, $interval) {
  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. isConnected: function() {
  11. return this.username != '';
  12. },
  13. addListener: function(f) {
  14. messageListeners.add(f);
  15. }
  16. };
  17. function wrap(func) {
  18. return function() {
  19. var args = arguments;
  20. $timeout(function() {
  21. func.apply(null, args);
  22. });
  23. }
  24. };
  25. service.connect = function(username) {
  26. service.error = null;
  27. chatSocket = new WS(jsRoutes.controllers.Application.chat(username).webSocketURL());
  28. chatSocket.onmessage = wrap(function(event) {
  29. var message = JSON.parse(event.data);
  30. if(message.error) {
  31. service.error = message.error;
  32. }
  33. if(message.kind != "pong") {
  34. $rootScope.$broadcast('ws:message', message);
  35. }
  36. });
  37. chatSocket.onopen = wrap(function() {
  38. $rootScope.$broadcast('ws:connected', username);
  39. service.username = username;
  40. ping = $interval(function() {
  41. service.send('/ping');
  42. }, 60000, 0, false);
  43. });
  44. chatSocket.onclose = wrap(function() {
  45. $rootScope.$broadcast('ws:disconnected');
  46. service.username = '';
  47. $interval.cancel(ping);
  48. });
  49. }
  50. service.disconnect = function() {
  51. chatSocket.close();
  52. chatSocket = null;
  53. }
  54. service.send = function(message) {
  55. chatSocket.send(JSON.stringify({text: message}));
  56. }
  57. return service;
  58. })
  59. .factory('Chat', function($rootScope, Connection) {
  60. var service = {
  61. username: '',
  62. messages: [],
  63. receive: function(message) {
  64. if(arguments.length == 3) {
  65. message = {
  66. kind: arguments[0],
  67. user: arguments[1],
  68. message: arguments[2]
  69. };
  70. }
  71. service.messages.push(message);
  72. },
  73. getError: function() {
  74. return Connection.error;
  75. },
  76. connect: Connection.connect,
  77. disconnect: Connection.disconnect,
  78. isConnected: Connection.isConnected,
  79. send: Connection.send
  80. };
  81. $rootScope.$on('ws:connected', function(event, username) {
  82. service.username = username;
  83. });
  84. $rootScope.$on('ws:message', function(event, message) {
  85. if(message.kind == "talk") {
  86. service.receive(message);
  87. }
  88. else if(message.kind == "join") {
  89. service.receive("join", message.user, " has joined.");
  90. }
  91. else if(message.kind == "quit") {
  92. service.receive("quit", message.user, " has left.");
  93. }
  94. });
  95. $rootScope.$on('ws:disconnected', function() {
  96. service.messages = [];
  97. service.username = '';
  98. });
  99. if(Connection.isConnected()) {
  100. service.username = Connection.username;
  101. }
  102. return service;
  103. })