socket.io-sessionid-patch.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2013 United States Government, as represented by the Secretary of Defense, Under
  2. // Secretary of Defense (Personnel & Readiness).
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. // in compliance with the License. You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software distributed under the License
  10. // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11. // or implied. See the License for the specific language governing permissions and limitations under
  12. // the License.
  13. ( function () {
  14. // Only patch if running older Socket.io version i.e. 0.6.3, Ruby server
  15. if ( parseFloat( io.version ) >= 0.7 ) {
  16. return;
  17. }
  18. var transport = io.Transport;
  19. var transports = [ transport, transport.websocket, transport.flashsocket ];
  20. // This overrides socket.io's Transport.onDisconnect that resets the sessionid on disconnect
  21. // We would like to keep it around so it can be reused on reconnect so the application understands that this
  22. // is an existing client reconnecting, instead of a new client
  23. transports.map( function( trans ) {
  24. if ( !trans ) {
  25. return;
  26. }
  27. trans.prototype.onDisconnect = function( message ){
  28. this.connecting = false;
  29. this.connected = false;
  30. this.base.onDisconnect();
  31. }
  32. } );
  33. // This overrides socket.io's onMessage functions to have it register that a connection has been made, even
  34. // when the sessionid is not null
  35. var xhrTransports = [ transport.XHR, transport.htmlfile, transport['xhr-multipart'],
  36. transport['xhr-polling'], transport['jsonp-polling'] ];
  37. transports = transports.concat( xhrTransports );
  38. transports.map( function( trans ) {
  39. if ( !trans ) {
  40. return;
  41. }
  42. var oldOnMessage = trans.prototype.onMessage;
  43. trans.prototype.onMessage = function( message ){
  44. oldOnMessage.call( this, message );
  45. if ( !this.connected ) {
  46. this.onConnect();
  47. }
  48. }
  49. } );
  50. } )();