PageRenderTime 34ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/web/Socket.IO/lib/socket.js

https://github.com/novemberborn/WriteOnWall
JavaScript | 161 lines | 136 code | 18 blank | 7 comment | 40 complexity | 7a9412b42671c325c90d6219ea23f255 MD5 | raw file
  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 Socket = io.Socket = function(host, options){
  10. this.host = host || document.domain;
  11. this.options = {
  12. secure: false,
  13. document: document,
  14. port: document.location.port || 80,
  15. resource: 'socket.io',
  16. transports: ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling'],
  17. transportOptions: {
  18. 'xhr-polling': {
  19. timeout: 25000 // based on polling duration default
  20. },
  21. 'jsonp-polling': {
  22. timeout: 25000
  23. }
  24. },
  25. connectTimeout: 5000,
  26. tryTransportsOnConnectTimeout: true,
  27. rememberTransport: true
  28. };
  29. io.util.merge(this.options, options);
  30. this.connected = false;
  31. this.connecting = false;
  32. this._events = {};
  33. this.transport = this.getTransport();
  34. if (!this.transport && 'console' in window) console.error('No transport available');
  35. };
  36. Socket.prototype.getTransport = function(override){
  37. var transports = override || this.options.transports, match;
  38. if (this.options.rememberTransport && !override){
  39. match = this.options.document.cookie.match('(?:^|;)\\s*socketio=([^;]*)');
  40. if (match){
  41. this._rememberedTransport = true;
  42. transports = [decodeURIComponent(match[1])];
  43. }
  44. }
  45. for (var i = 0, transport; transport = transports[i]; i++){
  46. if (io.Transport[transport]
  47. && io.Transport[transport].check()
  48. && (!this._isXDomain() || io.Transport[transport].xdomainCheck())){
  49. return new io.Transport[transport](this, this.options.transportOptions[transport] || {});
  50. }
  51. }
  52. return null;
  53. };
  54. Socket.prototype.connect = function(){
  55. if (this.transport && !this.connected){
  56. if (this.connecting) this.disconnect();
  57. this.connecting = true;
  58. this.transport.connect();
  59. if (this.options.connectTimeout){
  60. var self = this;
  61. this.connectTimeoutTimer = setTimeout(function(){
  62. if (!self.connected){
  63. self.disconnect();
  64. if (self.options.tryTransportsOnConnectTimeout && !self._rememberedTransport){
  65. var remainingTransports = [], transports = self.options.transports;
  66. for (var i = 0, transport; transport = transports[i]; i++){
  67. if (transport != self.transport.type) remainingTransports.push(transport);
  68. }
  69. if (remainingTransports.length){
  70. self.transport = self.getTransport(remainingTransports);
  71. self.connect();
  72. }
  73. }
  74. }
  75. }, this.options.connectTimeout);
  76. }
  77. }
  78. return this;
  79. };
  80. Socket.prototype.send = function(data){
  81. if (!this.transport || !this.transport.connected) return this._queue(data);
  82. this.transport.send(data);
  83. return this;
  84. };
  85. Socket.prototype.disconnect = function(){
  86. if (this.connectTimeoutTimer) clearTimeout(this.connectTimeoutTimer);
  87. this.transport.disconnect();
  88. return this;
  89. };
  90. Socket.prototype.on = function(name, fn){
  91. if (!(name in this._events)) this._events[name] = [];
  92. this._events[name].push(fn);
  93. return this;
  94. };
  95. Socket.prototype.emit = function(name, args){
  96. if (name in this._events){
  97. var events = this._events[name].concat();
  98. for (var i = 0, ii = events.length; i < ii; i++)
  99. events[i].apply(this, args === undefined ? [] : args);
  100. }
  101. return this;
  102. };
  103. Socket.prototype.removeEvent = function(name, fn){
  104. if (name in this._events){
  105. for (var a = 0, l = this._events[name].length; a < l; a++)
  106. if (this._events[name][a] == fn) this._events[name].splice(a, 1);
  107. }
  108. return this;
  109. };
  110. Socket.prototype._queue = function(message){
  111. if (!('_queueStack' in this)) this._queueStack = [];
  112. this._queueStack.push(message);
  113. return this;
  114. };
  115. Socket.prototype._doQueue = function(){
  116. if (!('_queueStack' in this) || !this._queueStack.length) return this;
  117. this.transport.send(this._queueStack);
  118. this._queueStack = [];
  119. return this;
  120. };
  121. Socket.prototype._isXDomain = function(){
  122. return this.host !== document.domain;
  123. };
  124. Socket.prototype._onConnect = function(){
  125. this.connected = true;
  126. this.connecting = false;
  127. this._doQueue();
  128. if (this.options.rememberTransport) this.options.document.cookie = 'socketio=' + encodeURIComponent(this.transport.type);
  129. this.emit('connect');
  130. };
  131. Socket.prototype._onMessage = function(data){
  132. this.emit('message', [data]);
  133. };
  134. Socket.prototype._onDisconnect = function(){
  135. var wasConnected = this.connected;
  136. this.connected = false;
  137. this.connecting = false;
  138. this._queueStack = [];
  139. if (wasConnected) this.emit('disconnect');
  140. };
  141. Socket.prototype.fire = Socket.prototype.emit;
  142. Socket.prototype.addListener = Socket.prototype.addEvent = Socket.prototype.addEventListener = Socket.prototype.on;
  143. })();