chatServices.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. chatSocket = new WS(jsRoutes.controllers.Application.chat(username).webSocketURL());
  27. chatSocket.onmessage = wrap(function(event) {
  28. var message = JSON.parse(event.data);
  29. console.log(message);
  30. if(message.kind != "pong") {
  31. $rootScope.$broadcast('ws:message', message);
  32. }
  33. });
  34. chatSocket.onopen = wrap(function() {
  35. $rootScope.$broadcast('ws:connected', username);
  36. service.username = username;
  37. ping = $interval(function() {
  38. service.send('/ping');
  39. }, 60000, 0, false);
  40. });
  41. chatSocket.onclose = wrap(function() {
  42. $rootScope.$broadcast('ws:disconnected');
  43. service.username = '';
  44. $interval.cancel(ping);
  45. });
  46. }
  47. service.disconnect = function() {
  48. chatSocket.close();
  49. chatSocket = null;
  50. }
  51. service.send = function(message) {
  52. chatSocket.send(JSON.stringify({text: message}));
  53. }
  54. return service;
  55. })
  56. .factory('Chat', function($rootScope, Connection) {
  57. var service = {
  58. username: '',
  59. messages: [],
  60. receive: function(message) {
  61. if(arguments.length == 3) {
  62. message = {
  63. kind: arguments[0],
  64. user: arguments[1],
  65. message: arguments[2]
  66. };
  67. }
  68. service.messages.push(message);
  69. },
  70. connect: Connection.connect,
  71. disconnect: Connection.disconnect,
  72. isConnected: Connection.isConnected,
  73. send: Connection.send
  74. };
  75. $rootScope.$on('ws:connected', function(event, username) {
  76. service.username = username;
  77. });
  78. $rootScope.$on('ws:message', function(event, message) {
  79. if(message.kind == "talk") {
  80. service.receive(message);
  81. }
  82. else if(message.kind == "join") {
  83. service.receive("join", message.user, " has joined.");
  84. }
  85. else if(message.kind == "quit") {
  86. service.receive("quit", message.user, " has left.");
  87. }
  88. });
  89. $rootScope.$on('ws:disconnected', function() {
  90. service.messages = [];
  91. service.username = '';
  92. });
  93. if(Connection.isConnected()) {
  94. service.username = Connection.username;
  95. }
  96. return service;
  97. })