/js/lib/Socket.IO-node/support/socket.io-client/lib/transports/websocket.js
JavaScript | 66 lines | 45 code | 13 blank | 8 comment | 6 complexity | 717b6fb9daf4b53a40f81023bb221291 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
- /**
- * Socket.IO client
- *
- * @author Guillermo Rauch <guillermo@learnboost.com>
- * @license The MIT license.
- * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
- */
- (function(){
- var io = this.io;
-
- var WS = io.Transport.websocket = function(){
- io.Transport.apply(this, arguments);
- };
-
- io.util.inherit(WS, io.Transport);
-
- WS.prototype.type = 'websocket';
-
- WS.prototype.connect = function(){
- var self = this;
- this.socket = new WebSocket(this._prepareUrl());
- this.socket.onmessage = function(ev){ self._onData(ev.data); };
- this.socket.onclose = function(ev){ self._onClose(); };
- this.socket.onerror = function(e){ self._onError(e); };
- return this;
- };
-
- WS.prototype.send = function(data){
- if (this.socket) this.socket.send(this._encode(data));
- return this;
- };
-
- WS.prototype.disconnect = function(){
- if (this.socket) this.socket.close();
- return this;
- };
-
- WS.prototype._onClose = function(){
- this._onDisconnect();
- return this;
- };
- WS.prototype._onError = function(e){
- this.base.emit('error', [e]);
- };
-
- WS.prototype._prepareUrl = function(){
- return (this.base.options.secure ? 'wss' : 'ws')
- + '://' + this.base.host
- + ':' + this.base.options.port
- + '/' + this.base.options.resource
- + '/' + this.type
- + (this.sessionid ? ('/' + this.sessionid) : '');
- };
-
- WS.check = function(){
- // we make sure WebSocket is not confounded with a previously loaded flash WebSocket
- return 'WebSocket' in window && WebSocket.prototype && ( WebSocket.prototype.send && !!WebSocket.prototype.send.toString().match(/native/i)) && typeof WebSocket !== "undefined";
- };
- WS.xdomainCheck = function(){
- return true;
- };
-
- })();