You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
7.3 KiB

4 years ago
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var FayeWebsocket, RawWebsocketSessionReceiver, Transport, WebSocketReceiver, transport, utils,
  4. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  5. hasProp = {}.hasOwnProperty;
  6. FayeWebsocket = require('faye-websocket');
  7. utils = require('./utils');
  8. transport = require('./transport');
  9. exports.app = {
  10. _websocket_check: function(req, connection, head) {
  11. if (!FayeWebsocket.isWebSocket(req)) {
  12. throw {
  13. status: 400,
  14. message: 'Not a valid websocket request'
  15. };
  16. }
  17. },
  18. sockjs_websocket: function(req, connection, head) {
  19. var ws;
  20. this._websocket_check(req, connection, head);
  21. ws = new FayeWebsocket(req, connection, head, null, this.options.faye_server_options);
  22. ws.onopen = (function(_this) {
  23. return function() {
  24. return transport.registerNoSession(req, _this, new WebSocketReceiver(ws, connection));
  25. };
  26. })(this);
  27. return true;
  28. },
  29. raw_websocket: function(req, connection, head) {
  30. var ver, ws;
  31. this._websocket_check(req, connection, head);
  32. ver = req.headers['sec-websocket-version'] || '';
  33. if (['8', '13'].indexOf(ver) === -1) {
  34. throw {
  35. status: 400,
  36. message: 'Only supported WebSocket protocol is RFC 6455.'
  37. };
  38. }
  39. ws = new FayeWebsocket(req, connection, head, null, this.options.faye_server_options);
  40. ws.onopen = (function(_this) {
  41. return function() {
  42. return new RawWebsocketSessionReceiver(req, connection, _this, ws);
  43. };
  44. })(this);
  45. return true;
  46. }
  47. };
  48. WebSocketReceiver = (function(superClass) {
  49. extend(WebSocketReceiver, superClass);
  50. WebSocketReceiver.prototype.protocol = "websocket";
  51. function WebSocketReceiver(ws1, connection1) {
  52. var x;
  53. this.ws = ws1;
  54. this.connection = connection1;
  55. try {
  56. this.connection.setKeepAlive(true, 5000);
  57. this.connection.setNoDelay(true);
  58. } catch (error) {
  59. x = error;
  60. }
  61. this.ws.addEventListener('message', (function(_this) {
  62. return function(m) {
  63. return _this.didMessage(m.data);
  64. };
  65. })(this));
  66. this.heartbeat_cb = (function(_this) {
  67. return function() {
  68. return _this.heartbeat_timeout();
  69. };
  70. })(this);
  71. WebSocketReceiver.__super__.constructor.call(this, this.connection);
  72. }
  73. WebSocketReceiver.prototype.setUp = function() {
  74. WebSocketReceiver.__super__.setUp.apply(this, arguments);
  75. return this.ws.addEventListener('close', this.thingy_end_cb);
  76. };
  77. WebSocketReceiver.prototype.tearDown = function() {
  78. this.ws.removeEventListener('close', this.thingy_end_cb);
  79. return WebSocketReceiver.__super__.tearDown.apply(this, arguments);
  80. };
  81. WebSocketReceiver.prototype.didMessage = function(payload) {
  82. var i, len, message, msg, results, x;
  83. if (this.ws && this.session && payload.length > 0) {
  84. try {
  85. message = JSON.parse(payload);
  86. } catch (error) {
  87. x = error;
  88. return this.didClose(3000, 'Broken framing.');
  89. }
  90. if (payload[0] === '[') {
  91. results = [];
  92. for (i = 0, len = message.length; i < len; i++) {
  93. msg = message[i];
  94. results.push(this.session.didMessage(msg));
  95. }
  96. return results;
  97. } else {
  98. return this.session.didMessage(message);
  99. }
  100. }
  101. };
  102. WebSocketReceiver.prototype.doSendFrame = function(payload) {
  103. var x;
  104. if (this.ws) {
  105. try {
  106. this.ws.send(payload);
  107. return true;
  108. } catch (error) {
  109. x = error;
  110. }
  111. }
  112. return false;
  113. };
  114. WebSocketReceiver.prototype.didClose = function(status, reason) {
  115. var x;
  116. if (status == null) {
  117. status = 1000;
  118. }
  119. if (reason == null) {
  120. reason = "Normal closure";
  121. }
  122. WebSocketReceiver.__super__.didClose.apply(this, arguments);
  123. try {
  124. this.ws.close(status, reason, false);
  125. } catch (error) {
  126. x = error;
  127. }
  128. this.ws = null;
  129. return this.connection = null;
  130. };
  131. WebSocketReceiver.prototype.heartbeat = function() {
  132. var hto_ref, supportsHeartbeats;
  133. supportsHeartbeats = this.ws.ping(null, function() {
  134. return clearTimeout(hto_ref);
  135. });
  136. if (supportsHeartbeats) {
  137. return hto_ref = setTimeout(this.heartbeat_cb, 10000);
  138. } else {
  139. return WebSocketReceiver.__super__.heartbeat.apply(this, arguments);
  140. }
  141. };
  142. WebSocketReceiver.prototype.heartbeat_timeout = function() {
  143. if (this.session != null) {
  144. return this.session.close(3000, 'No response from heartbeat');
  145. }
  146. };
  147. return WebSocketReceiver;
  148. })(transport.GenericReceiver);
  149. Transport = transport.Transport;
  150. RawWebsocketSessionReceiver = (function(superClass) {
  151. extend(RawWebsocketSessionReceiver, superClass);
  152. function RawWebsocketSessionReceiver(req, conn, server, ws1) {
  153. this.ws = ws1;
  154. this.prefix = server.options.prefix;
  155. this.readyState = Transport.OPEN;
  156. this.recv = {
  157. connection: conn,
  158. protocol: "websocket-raw"
  159. };
  160. this.connection = new transport.SockJSConnection(this);
  161. this.decorateConnection(req);
  162. server.emit('connection', this.connection);
  163. this._end_cb = (function(_this) {
  164. return function() {
  165. return _this.didClose();
  166. };
  167. })(this);
  168. this.ws.addEventListener('close', this._end_cb);
  169. this._message_cb = (function(_this) {
  170. return function(m) {
  171. return _this.didMessage(m);
  172. };
  173. })(this);
  174. this.ws.addEventListener('message', this._message_cb);
  175. }
  176. RawWebsocketSessionReceiver.prototype.didMessage = function(m) {
  177. if (this.readyState === Transport.OPEN) {
  178. this.connection.emit('data', m.data);
  179. }
  180. };
  181. RawWebsocketSessionReceiver.prototype.send = function(payload) {
  182. if (this.readyState !== Transport.OPEN) {
  183. return false;
  184. }
  185. this.ws.send(payload);
  186. return true;
  187. };
  188. RawWebsocketSessionReceiver.prototype.close = function(status, reason) {
  189. if (status == null) {
  190. status = 1000;
  191. }
  192. if (reason == null) {
  193. reason = "Normal closure";
  194. }
  195. if (this.readyState !== Transport.OPEN) {
  196. return false;
  197. }
  198. this.readyState = Transport.CLOSING;
  199. this.ws.close(status, reason, false);
  200. return true;
  201. };
  202. RawWebsocketSessionReceiver.prototype.didClose = function() {
  203. var x;
  204. if (!this.ws) {
  205. return;
  206. }
  207. this.ws.removeEventListener('message', this._message_cb);
  208. this.ws.removeEventListener('close', this._end_cb);
  209. try {
  210. this.ws.close(1000, "Normal closure", false);
  211. } catch (error) {
  212. x = error;
  213. }
  214. this.ws = null;
  215. this.readyState = Transport.CLOSED;
  216. this.connection.emit('end');
  217. this.connection.emit('close');
  218. return this.connection = null;
  219. };
  220. return RawWebsocketSessionReceiver;
  221. })(transport.Session);
  222. }).call(this);