1
0

chatServices.js 3.2 KB

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