/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
1/** 2 * Socket.IO client 3 * 4 * @author Guillermo Rauch <guillermo@learnboost.com> 5 * @license The MIT license. 6 * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com> 7 */ 8 9(function(){ 10 var io = this.io; 11 12 var WS = io.Transport.websocket = function(){ 13 io.Transport.apply(this, arguments); 14 }; 15 16 io.util.inherit(WS, io.Transport); 17 18 WS.prototype.type = 'websocket'; 19 20 WS.prototype.connect = function(){ 21 var self = this; 22 this.socket = new WebSocket(this._prepareUrl()); 23 this.socket.onmessage = function(ev){ self._onData(ev.data); }; 24 this.socket.onclose = function(ev){ self._onClose(); }; 25 this.socket.onerror = function(e){ self._onError(e); }; 26 return this; 27 }; 28 29 WS.prototype.send = function(data){ 30 if (this.socket) this.socket.send(this._encode(data)); 31 return this; 32 }; 33 34 WS.prototype.disconnect = function(){ 35 if (this.socket) this.socket.close(); 36 return this; 37 }; 38 39 WS.prototype._onClose = function(){ 40 this._onDisconnect(); 41 return this; 42 }; 43 44 WS.prototype._onError = function(e){ 45 this.base.emit('error', [e]); 46 }; 47 48 WS.prototype._prepareUrl = function(){ 49 return (this.base.options.secure ? 'wss' : 'ws') 50 + '://' + this.base.host 51 + ':' + this.base.options.port 52 + '/' + this.base.options.resource 53 + '/' + this.type 54 + (this.sessionid ? ('/' + this.sessionid) : ''); 55 }; 56 57 WS.check = function(){ 58 // we make sure WebSocket is not confounded with a previously loaded flash WebSocket 59 return 'WebSocket' in window && WebSocket.prototype && ( WebSocket.prototype.send && !!WebSocket.prototype.send.toString().match(/native/i)) && typeof WebSocket !== "undefined"; 60 }; 61 62 WS.xdomainCheck = function(){ 63 return true; 64 }; 65 66})();