PageRenderTime 65ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/socket.io-client/lib/transports/websocket.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
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. (function(){
  9. var io = this.io;
  10. var WS = io.Transport.websocket = function(){
  11. io.Transport.apply(this, arguments);
  12. };
  13. io.util.inherit(WS, io.Transport);
  14. WS.prototype.type = 'websocket';
  15. WS.prototype.connect = function(){
  16. var self = this;
  17. this.socket = new WebSocket(this._prepareUrl());
  18. this.socket.onmessage = function(ev){ self._onData(ev.data); };
  19. this.socket.onclose = function(ev){ self._onClose(); };
  20. this.socket.onerror = function(e){ self._onError(e); };
  21. return this;
  22. };
  23. WS.prototype.send = function(data){
  24. if (this.socket) this.socket.send(this._encode(data));
  25. return this;
  26. };
  27. WS.prototype.disconnect = function(){
  28. if (this.socket) this.socket.close();
  29. return this;
  30. };
  31. WS.prototype._onClose = function(){
  32. this._onDisconnect();
  33. return this;
  34. };
  35. WS.prototype._onError = function(e){
  36. this.base.emit('error', [e]);
  37. };
  38. WS.prototype._prepareUrl = function(){
  39. return (this.base.options.secure ? 'wss' : 'ws')
  40. + '://' + this.base.host
  41. + ':' + this.base.options.port
  42. + '/' + this.base.options.resource
  43. + '/' + this.type
  44. + (this.sessionid ? ('/' + this.sessionid) : '');
  45. };
  46. WS.check = function(){
  47. // we make sure WebSocket is not confounded with a previously loaded flash WebSocket
  48. return 'WebSocket' in window && WebSocket.prototype && ( WebSocket.prototype.send && !!WebSocket.prototype.send.toString().match(/native/i)) && typeof WebSocket !== "undefined";
  49. };
  50. WS.xdomainCheck = function(){
  51. return true;
  52. };
  53. })();