Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Socket.IO 0.6.2 - Built with build.js */
- /**
- * Socket.IO client
- *
- * @author Guillermo Rauch <guillermo@learnboost.com>
- * @license The MIT license.
- * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
- */
- this.io = {
- version: '0.6.2',
- setPath: function(path){
- if (window.console && console.error) console.error('io.setPath will be removed. Please set the variable WEB_SOCKET_SWF_LOCATION pointing to WebSocketMain.swf');
- this.path = /\/$/.test(path) ? path : path + '/';
- WEB_SOCKET_SWF_LOCATION = path + 'lib/vendor/web-socket-js/WebSocketMain.swf';
- }
- };
- if ('jQuery' in this) jQuery.io = this.io;
- if (typeof window != 'undefined'){
- // WEB_SOCKET_SWF_LOCATION = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//cdn.socket.io/' + this.io.version + '/WebSocketMain.swf';
- if (typeof WEB_SOCKET_SWF_LOCATION === 'undefined')
- WEB_SOCKET_SWF_LOCATION = '/socket.io/lib/vendor/web-socket-js/WebSocketMain.swf';
- }
- /**
- * Socket.IO client
- *
- * @author Guillermo Rauch <guillermo@learnboost.com>
- * @license The MIT license.
- * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
- */
- (function(){
- var _pageLoaded = false;
- io.util = {
- ios: false,
- load: function(fn){
- if (/loaded|complete/.test(document.readyState) || _pageLoaded) return fn();
- if ('attachEvent' in window){
- window.attachEvent('onload', fn);
- } else {
- window.addEventListener('load', fn, false);
- }
- },
- inherit: function(ctor, superCtor){
- // no support for `instanceof` for now
- for (var i in superCtor.prototype){
- ctor.prototype[i] = superCtor.prototype[i];
- }
- },
- indexOf: function(arr, item, from){
- for (var l = arr.length, i = (from < 0) ? Math.max(0, l + from) : from || 0; i < l; i++){
- if (arr[i] === item) return i;
- }
- return -1;
- },
- isArray: function(obj){
- return Object.prototype.toString.call(obj) === '[object Array]';
- },
- merge: function(target, additional){
- for (var i in additional)
- if (additional.hasOwnProperty(i))
- target[i] = additional[i];
- }
- };
- io.util.ios = /iphone|ipad/i.test(navigator.userAgent);
- io.util.android = /android/i.test(navigator.userAgent);
- io.util.opera = /opera/i.test(navigator.userAgent);
- io.util.load(function(){
- _pageLoaded = true;
- });
- })();
- /**
- * Socket.IO client
- *
- * @author Guillermo Rauch <guillermo@learnboost.com>
- * @license The MIT license.
- * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
- */
- // abstract
- (function(){
- var frame = '~m~',
- stringify = function(message){
- if (Object.prototype.toString.call(message) == '[object Object]'){
- if (!('JSON' in window)){
- if ('console' in window && console.error) console.error('Trying to encode as JSON, but JSON.stringify is missing.');
- return '{ "$error": "Invalid message" }';
- }
- return '~j~' + JSON.stringify(message);
- } else {
- return String(message);
- }
- };
- Transport = io.Transport = function(base, options){
- this.base = base;
- this.options = {
- timeout: 15000 // based on heartbeat interval default
- };
- io.util.merge(this.options, options);
- };
- Transport.prototype.send = function(){
- throw new Error('Missing send() implementation');
- };
- Transport.prototype.connect = function(){
- throw new Error('Missing connect() implementation');
- };
- Transport.prototype.disconnect = function(){
- throw new Error('Missing disconnect() implementation');
- };
- Transport.prototype._encode = function(messages){
- var ret = '', message,
- messages = io.util.isArray(messages) ? messages : [messages];
- for (var i = 0, l = messages.length; i < l; i++){
- message = messages[i] === null || messages[i] === undefined ? '' : stringify(messages[i]);
- ret += frame + message.length + frame + message;
- }
- return ret;
- };
- Transport.prototype._decode = function(data){
- var messages = [], number, n;
- do {
- if (data.substr(0, 3) !== frame) return messages;
- data = data.substr(3);
- number = '', n = '';
- for (var i = 0, l = data.length; i < l; i++){
- n = Number(data.substr(i, 1));
- if (data.substr(i, 1) == n){
- number += n;
- } else {
- data = data.substr(number.length + frame.length);
- number = Number(number);
- break;
- }
- }
- messages.push(data.substr(0, number)); // here
- data = data.substr(number);
- } while(data !== '');
- return messages;
- };
- Transport.prototype._onData = function(data){
- this._setTimeout();
- var msgs = this._decode(data);
- if (msgs && msgs.length){
- for (var i = 0, l = msgs.length; i < l; i++){
- this._onMessage(msgs[i]);
- }
- }
- };
- Transport.prototype._setTimeout = function(){
- var self = this;
- if (this._timeout) clearTimeout(this._timeout);
- this._timeout = setTimeout(function(){
- self._onTimeout();
- }, this.options.timeout);
- };
- Transport.prototype._onTimeout = function(){
- this._onDisconnect();
- };
- Transport.prototype._onMessage = function(message){
- if (!this.sessionid){
- this.sessionid = message;
- this._onConnect();
- } else if (message.substr(0, 3) == '~h~'){
- this._onHeartbeat(message.substr(3));
- } else if (message.substr(0, 3) == '~j~'){
- this.base._onMessage(JSON.parse(message.substr(3)));
- } else {
- this.base._onMessage(message);
- }
- },
- Transport.prototype._onHeartbeat = function(heartbeat){
- this.send('~h~' + heartbeat); // echo
- };
- Transport.prototype._onConnect = function(){
- this.connected = true;
- this.connecting = false;
- this.base._onConnect();
- this._setTimeout();
- };
- Transport.prototype._onDisconnect = function(){
- this.connecting = false;
- this.connected = false;
- this.sessionid = null;
- this.base._onDisconnect();
- };
- Transport.prototype._prepareUrl = function(){
- return (this.base.options.secure ? 'https' : 'http')
- + '://' + this.base.host
- + ':' + this.base.options.port
- + '/' + this.base.options.resource
- + '/' + this.type
- + (this.sessionid ? ('/' + this.sessionid) : '/');
- };
- })();
- /**
- * Socket.IO client
- *
- * @author Guillermo Rauch <guillermo@learnboost.com>
- * @license The MIT license.
- * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
- */
- (function(){
- var empty = new Function,
- XMLHttpRequestCORS = (function(){
- if (!('XMLHttpRequest' in window)) return false;
- // CORS feature detection
- var a = new XMLHttpRequest();
- return a.withCredentials != undefined;
- })(),
- request = function(xdomain){
- if ('XDomainRequest' in window && xdomain) return new XDomainRequest();
- if ('XMLHttpRequest' in window && (!xdomain || XMLHttpRequestCORS)) return new XMLHttpRequest();
- if (!xdomain){
- try {
- var a = new ActiveXObject('MSXML2.XMLHTTP');
- return a;
- } catch(e){}
- try {
- var b = new ActiveXObject('Microsoft.XMLHTTP');
- return b;
- } catch(e){}
- }
- return false;
- },
- XHR = io.Transport.XHR = function(){
- io.Transport.apply(this, arguments);
- this._sendBuffer = [];
- };
- io.util.inherit(XHR, io.Transport);
- XHR.prototype.connect = function(){
- this._get();
- return this;
- };
- XHR.prototype._checkSend = function(){
- if (!this._posting && this._sendBuffer.length){
- var encoded = this._encode(this._sendBuffer);
- this._sendBuffer = [];
- this._send(encoded);
- }
- };
- XHR.prototype.send = function(data){
- if (io.util.isArray(data)){
- this._sendBuffer.push.apply(this._sendBuffer, data);
- } else {
- this._sendBuffer.push(data);
- }
- this._checkSend();
- return this;
- };
- XHR.prototype._send = function(data){
- var self = this;
- this._posting = true;
- this._sendXhr = this._request('send', 'POST');
- this._sendXhr.onreadystatechange = function(){
- var status;
- if (self._sendXhr.readyState == 4){
- self._sendXhr.onreadystatechange = empty;
- try { status = self._sendXhr.status; } catch(e){}
- self._posting = false;
- if (status == 200){
- self._checkSend();
- } else {
- self._onDisconnect();
- }
- }
- };
- this._sendXhr.send('data=' + encodeURIComponent(data));
- };
- XHR.prototype.disconnect = function(){
- // send disconnection signal
- this._onDisconnect();
- return this;
- };
- XHR.prototype._onDisconnect = function(){
- if (this._xhr){
- this._xhr.onreadystatechange = empty;
- try {
- this._xhr.abort();
- } catch(e){}
- this._xhr = null;
- }
- if (this._sendXhr){
- this._sendXhr.onreadystatechange = empty;
- try {
- this._sendXhr.abort();
- } catch(e){}
- this._sendXhr = null;
- }
- this._sendBuffer = [];
- io.Transport.prototype._onDisconnect.call(this);
- };
- XHR.prototype._request = function(url, method, multipart){
- var req = request(this.base._isXDomain());
- if (multipart) req.multipart = true;
- req.open(method || 'GET', this._prepareUrl() + (url ? '/' + url : ''));
- if (method == 'POST' && 'setRequestHeader' in req){
- req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=utf-8');
- }
- return req;
- };
- XHR.check = function(xdomain){
- try {
- if (request(xdomain)) return true;
- } catch(e){}
- return false;
- };
- XHR.xdomainCheck = function(){
- return XHR.check(true);
- };
- XHR.request = request;
- })();
- /**
- * Socket.IO client
- *
- * @author Guillermo Rauch <guillermo@learnboost.com>
- * @license The MIT license.
- * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
- */
- (function(){
- 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;
- };
- })();
- /**
- * Socket.IO client
- *
- * @author Guillermo Rauch <guillermo@learnboost.com>
- * @license The MIT license.
- * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
- */
- (function(){
- var Flashsocket = io.Transport.flashsocket = function(){
- io.Transport.websocket.apply(this, arguments);
- };
- io.util.inherit(Flashsocket, io.Transport.websocket);
- Flashsocket.prototype.type = 'flashsocket';
- Flashsocket.prototype.connect = function(){
- var self = this, args = arguments;
- WebSocket.__addTask(function(){
- io.Transport.websocket.prototype.connect.apply(self, args);
- });
- return this;
- };
- Flashsocket.prototype.send = function(){
- var self = this, args = arguments;
- WebSocket.__addTask(function(){
- io.Transport.websocket.proto
Add Comment
Please, Sign In to add comment