From b60df56157ee1fd0bd4938799bac05a62fda91a1 Mon Sep 17 00:00:00 2001 From: lookshe Date: Sat, 14 Mar 2015 20:45:20 +0100 Subject: initial commit from working version --- .../lib/transports/websocket.js | 158 +++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 signaling-server/node_modules/socket.io/node_modules/socket.io-client/components/learnboost-engine.io-client/lib/transports/websocket.js (limited to 'signaling-server/node_modules/socket.io/node_modules/socket.io-client/components/learnboost-engine.io-client/lib/transports/websocket.js') diff --git a/signaling-server/node_modules/socket.io/node_modules/socket.io-client/components/learnboost-engine.io-client/lib/transports/websocket.js b/signaling-server/node_modules/socket.io/node_modules/socket.io-client/components/learnboost-engine.io-client/lib/transports/websocket.js new file mode 100644 index 0000000..1bf055d --- /dev/null +++ b/signaling-server/node_modules/socket.io/node_modules/socket.io-client/components/learnboost-engine.io-client/lib/transports/websocket.js @@ -0,0 +1,158 @@ + +/** + * Module dependencies. + */ + +var Transport = require('../transport') + , parser = require('../parser') + , util = require('../util') + , debug = require('debug')('engine.io-client:websocket'); + +/** + * Module exports. + */ + +module.exports = WS; + +/** + * Global reference. + */ + +var global = 'undefined' != typeof window ? window : global; + +/** + * WebSocket transport constructor. + * + * @api {Object} connection options + * @api public + */ + +function WS(opts){ + Transport.call(this, opts); +}; + +/** + * Inherits from Transport. + */ + +util.inherits(WS, Transport); + +/** + * Transport name. + * + * @api public + */ + +WS.prototype.name = 'websocket'; + +/** + * Opens socket. + * + * @api private + */ + +WS.prototype.doOpen = function(){ + if (!this.check()) { + // let probe timeout + return; + } + + var self = this; + + this.socket = new (ws())(this.uri()); + this.socket.onopen = function(){ + self.onOpen(); + }; + this.socket.onclose = function(){ + self.onClose(); + }; + this.socket.onmessage = function(ev){ + self.onData(ev.data); + }; + this.socket.onerror = function(e){ + self.onError('websocket error', e); + }; +}; + +/** + * Writes data to socket. + * + * @param {Array} array of packets. + * @api private + */ + +WS.prototype.write = function(packets){ + for (var i = 0, l = packets.length; i < l; i++) { + this.socket.send(parser.encodePacket(packets[i])); + } +}; + +/** + * Closes socket. + * + * @api private + */ + +WS.prototype.doClose = function(){ + if (typeof this.socket !== 'undefined') { + this.socket.close(); + } +}; + +/** + * Generates uri for connection. + * + * @api private + */ + +WS.prototype.uri = function(){ + var query = this.query || {}; + var schema = this.secure ? 'wss' : 'ws'; + var port = ''; + + // avoid port if default for schema + if (this.port && (('wss' == schema && this.port != 443) + || ('ws' == schema && this.port != 80))) { + port = ':' + this.port; + } + + // append timestamp to URI + if (this.timestampRequests) { + query[this.timestampParam] = +new Date; + } + + query = util.qs(query); + + // prepend ? to query + if (query.length) { + query = '?' + query; + } + + return schema + '://' + this.host + port + this.path + query; +}; + +/** + * Feature detection for WebSocket. + * + * @return {Boolean} whether this transport is available. + * @api public + */ + +WS.prototype.check = function(){ + var websocket = ws(); + return !!websocket && !('__initialize' in websocket && this.name === WS.prototype.name); +}; + +/** + * Getter for WS constructor. + * + * @api private + */ + +function ws(){ + if ('undefined' != typeof process) { + return require('ws'); + } + + return global.WebSocket || global.MozWebSocket; +} -- cgit v1.2.3