chatServices.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. $rootScope.$broadcast('chat:message', message);
  94. },
  95. send: Connection.send
  96. };
  97. $rootScope.$on('ws:connected', function(event, username) {
  98. service.username = username;
  99. });
  100. $rootScope.$on('ws:message', function(event, message) {
  101. if(message.kind == "talk") {
  102. service.receive(message);
  103. }
  104. else if(message.kind == "join") {
  105. service.receive("join", message.user, " has joined.");
  106. }
  107. else if(message.kind == "quit") {
  108. service.receive("quit", message.user, " has left.");
  109. }
  110. });
  111. $rootScope.$on('ws:disconnected', function() {
  112. service.messages = [];
  113. service.username = '';
  114. });
  115. if(Connection.isConnected()) {
  116. service.username = Connection.username;
  117. }
  118. return service;
  119. })