source: trip-planner-front/node_modules/sockjs-client/lib/transport/websocket.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1'use strict';
2
3var utils = require('../utils/event')
4 , urlUtils = require('../utils/url')
5 , inherits = require('inherits')
6 , EventEmitter = require('events').EventEmitter
7 , WebsocketDriver = require('./driver/websocket')
8 ;
9
10var debug = function() {};
11if (process.env.NODE_ENV !== 'production') {
12 debug = require('debug')('sockjs-client:websocket');
13}
14
15function WebSocketTransport(transUrl, ignore, options) {
16 if (!WebSocketTransport.enabled()) {
17 throw new Error('Transport created when disabled');
18 }
19
20 EventEmitter.call(this);
21 debug('constructor', transUrl);
22
23 var self = this;
24 var url = urlUtils.addPath(transUrl, '/websocket');
25 if (url.slice(0, 5) === 'https') {
26 url = 'wss' + url.slice(5);
27 } else {
28 url = 'ws' + url.slice(4);
29 }
30 this.url = url;
31
32 this.ws = new WebsocketDriver(this.url, [], options);
33 this.ws.onmessage = function(e) {
34 debug('message event', e.data);
35 self.emit('message', e.data);
36 };
37 // Firefox has an interesting bug. If a websocket connection is
38 // created after onunload, it stays alive even when user
39 // navigates away from the page. In such situation let's lie -
40 // let's not open the ws connection at all. See:
41 // https://github.com/sockjs/sockjs-client/issues/28
42 // https://bugzilla.mozilla.org/show_bug.cgi?id=696085
43 this.unloadRef = utils.unloadAdd(function() {
44 debug('unload');
45 self.ws.close();
46 });
47 this.ws.onclose = function(e) {
48 debug('close event', e.code, e.reason);
49 self.emit('close', e.code, e.reason);
50 self._cleanup();
51 };
52 this.ws.onerror = function(e) {
53 debug('error event', e);
54 self.emit('close', 1006, 'WebSocket connection broken');
55 self._cleanup();
56 };
57}
58
59inherits(WebSocketTransport, EventEmitter);
60
61WebSocketTransport.prototype.send = function(data) {
62 var msg = '[' + data + ']';
63 debug('send', msg);
64 this.ws.send(msg);
65};
66
67WebSocketTransport.prototype.close = function() {
68 debug('close');
69 var ws = this.ws;
70 this._cleanup();
71 if (ws) {
72 ws.close();
73 }
74};
75
76WebSocketTransport.prototype._cleanup = function() {
77 debug('_cleanup');
78 var ws = this.ws;
79 if (ws) {
80 ws.onmessage = ws.onclose = ws.onerror = null;
81 }
82 utils.unloadDel(this.unloadRef);
83 this.unloadRef = this.ws = null;
84 this.removeAllListeners();
85};
86
87WebSocketTransport.enabled = function() {
88 debug('enabled');
89 return !!WebsocketDriver;
90};
91WebSocketTransport.transportName = 'websocket';
92
93// In theory, ws should require 1 round trip. But in chrome, this is
94// not very stable over SSL. Most likely a ws connection requires a
95// separate SSL connection, in which case 2 round trips are an
96// absolute minumum.
97WebSocketTransport.roundTrips = 2;
98
99module.exports = WebSocketTransport;
Note: See TracBrowser for help on using the repository browser.