Created
September 24, 2010 20:05
-
-
Save kriszyp/595947 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dojo.provide("dojo.socket"); | |
dojo.socket = function(/*dojo.__XhrArgs*/ argsOrUrl){ | |
// summary: | |
// Provides a simple socket connection using WebSocket or long-polling based | |
// communication in legacy browsers for comet-style communication. This is based | |
// on the WebSocket API and returns an object that implements the WebSocket interface: | |
// http://dev.w3.org/html5/websockets/#websocket | |
// argsOrUrl: | |
// This uses the same arguments as the other I/O functions in Dojo, or a | |
// URL to connect to. Several additional properties are supported: | |
// argsOrUrl.transport: | |
// Provide an alternate transport like dojo.io.script.get | |
// argsOrUrl.reconnectTime | |
// Will attempt to reconnect after a closed connection after a given amount of time | |
// Specify -1 to prevent reconnection | |
// returns: | |
// An object that implements the WebSocket API | |
// example: | |
// | var socket = dojo.socket({url:"/comet"}); | |
// | dojo.connect(socket, "onmessage", function(event){ | |
// | var message = event.data; | |
// | // do something with the message | |
// | }); | |
// | whenDone(function(){ | |
// | socket.close(); | |
// | }); | |
if(typeof argsOrUrl == "string"){ | |
argsOrUrl = {url: argsOrUrl}; | |
} | |
var deferred, socket, reconnectTime = argsOrUrl.reconnectTime || 10000; | |
function connect(){ | |
if(typeof WebSocket == "undefined" || argsOrUrl.noWebSocket){ | |
socket = { | |
send: function(data){ | |
(argsOrUrl.transport ? | |
argsOrUrl.transport(argsOrUrl) : | |
dojo.xhr(argsOrUrl.method || "POST", args)).then(null, onError); | |
}, | |
close: function(){ | |
reconnectTime = -1; | |
deferred.cancel(); | |
} | |
}; | |
deferred = argsOrUrl.transport ? | |
argsOrUrl.transport(argsOrUrl) : | |
dojo.xhr(argsOrUrl.method || "POST", args); | |
deferred.then(function(response){ | |
connect(); | |
setTimeout(function(){ | |
if(socket.onmessage){ | |
socket.onmessage({ | |
name: "message", | |
data: response, | |
ioArgs: deferred.ioArgs | |
}); | |
} | |
}); | |
}, onError); | |
return socket; | |
} | |
var ws = new WebSocket(argsOrUrl.url); | |
if(reconnectTime > -1){ | |
ws.addEventListener("close", onClose); | |
} | |
return ws; | |
} | |
function onError(error){ | |
setTimeout(function(){ | |
if(socket.onerror){ | |
socket.onerror({ | |
name: "error", | |
error: error, | |
ioArgs: deferred.ioArgs | |
}); | |
} | |
}); | |
onClose({ | |
name: "close", | |
wasClean: false, | |
ioArgs: deferred.ioArgs | |
}); | |
} | |
function onClose(event){ | |
if(reconnectTime > -1){ | |
setTimeout(connect, reconnectTime); | |
}else{ | |
setTimeout(function(){ | |
if(socket && socket.onclose){ | |
socket.onclose(event); | |
} | |
}); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment