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