1
0

chatServices.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. 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. function makeid() {
  26. var text = "";
  27. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  28. for( var i=0; i < 5; i++ )
  29. text += possible.charAt(Math.floor(Math.random() * possible.length));
  30. return text;
  31. }
  32. function getRoom() {
  33. if(!$location.path()) {
  34. $location.path(makeid());
  35. }
  36. return $location.path();
  37. }
  38. service.connect = function(username) {
  39. service.error = null;
  40. chatSocket = new WS(jsRoutes.controllers.Application.chat(username, getRoom()).webSocketURL());
  41. chatSocket.onmessage = wrap(function(event) {
  42. var message = JSON.parse(event.data);
  43. if(message.error) {
  44. service.error = message.error;
  45. }
  46. if(message.kind != "pong") {
  47. $rootScope.$broadcast('ws:message', message);
  48. }
  49. });
  50. chatSocket.onopen = wrap(function() {
  51. $rootScope.$broadcast('ws:connected', username);
  52. service.username = username;
  53. ping = $interval(function() {
  54. service.send('/ping');
  55. }, 60000, 0, false);
  56. });
  57. chatSocket.onclose = wrap(function() {
  58. $rootScope.$broadcast('ws:disconnected');
  59. service.username = '';
  60. $interval.cancel(ping);
  61. });
  62. }
  63. service.disconnect = function() {
  64. chatSocket.close();
  65. chatSocket = null;
  66. }
  67. service.send = function(message) {
  68. chatSocket.send(JSON.stringify({text: message}));
  69. }
  70. return service;
  71. })
  72. .factory('Chat', function($rootScope, Connection) {
  73. var service = {
  74. username: '',
  75. messages: [],
  76. receive: function(message) {
  77. if(arguments.length == 3) {
  78. message = {
  79. kind: arguments[0],
  80. user: arguments[1],
  81. message: arguments[2]
  82. };
  83. }
  84. service.messages.push(message);
  85. },
  86. getError: function() {
  87. return Connection.error;
  88. },
  89. connect: Connection.connect,
  90. disconnect: Connection.disconnect,
  91. isConnected: Connection.isConnected,
  92. send: Connection.send
  93. };
  94. $rootScope.$on('ws:connected', function(event, username) {
  95. service.username = username;
  96. });
  97. $rootScope.$on('ws:message', function(event, message) {
  98. if(message.kind == "talk") {
  99. service.receive(message);
  100. }
  101. else if(message.kind == "join") {
  102. service.receive("join", message.user, " has joined.");
  103. }
  104. else if(message.kind == "quit") {
  105. service.receive("quit", message.user, " has left.");
  106. }
  107. });
  108. $rootScope.$on('ws:disconnected', function() {
  109. service.messages = [];
  110. service.username = '';
  111. });
  112. if(Connection.isConnected()) {
  113. service.username = Connection.username;
  114. }
  115. return service;
  116. })