| 1 | define([ |
| 2 | "dojo/_base/array", |
| 3 | "dojo/_base/lang", |
| 4 | "dojo/_base/xhr", |
| 5 | "dojo/aspect", |
| 6 | "dojo/on", |
| 7 | "dojo/Evented", |
| 8 | "dojo/_base/url" |
| 9 | ], function(array, lang, xhr, aspect, on, Evented, dBaseUrl) { |
| 10 | |
| 11 | var WebSocket = window.WebSocket; |
| 12 | |
| 13 | var Socket = function(/*dojo.__XhrArgs*/ argsOrUrl){ |
| 14 | // summary: |
| 15 | // Provides a simple socket connection using WebSocket, or alternate |
| 16 | // communication mechanisms in legacy browsers for comet-style communication. This is based |
| 17 | // on the WebSocket API and returns an object that implements the WebSocket interface: |
| 18 | // http://dev.w3.org/html5/websockets/#websocket |
| 19 | // description: |
| 20 | // Provides socket connections. This can be used with virtually any Comet protocol. |
| 21 | // argsOrUrl: |
| 22 | // This uses the same arguments as the other I/O functions in Dojo, or a |
| 23 | // URL to connect to. The URL should be a relative URL in order to properly |
| 24 | // work with WebSockets (it can still be host relative, like //other-site.org/endpoint) |
| 25 | // returns: |
| 26 | // An object that implements the WebSocket API |
| 27 | // example: |
| 28 | // | require(["dojox/socket", "dojo/aspect"], function(socket, aspect) { |
| 29 | // | var sock = socket({"url://comet-server/comet"); |
| 30 | // | // we could also add auto-reconnect support |
| 31 | // | // now we can connect to standard HTML5 WebSocket-style events |
| 32 | // | aspect.after(socket, "onmessage", function(event){ |
| 33 | // | var message = event.data; |
| 34 | // | // do something with the message |
| 35 | // | }); |
| 36 | // | // send something |
| 37 | // | sock.send("hi there"); |
| 38 | // | ... |
| 39 | // | }); |
| 40 | // You can also use the Reconnect module: |
| 41 | // | require["dojox/socket", "dojox/socket/Reconnect"], function(dxSocket, reconnect){ |
| 42 | // | var socket = dxSocket({url:"/comet"}); |
| 43 | // | // add auto-reconnect support |
| 44 | // | socket = reconnect(socket); |
| 45 | if(typeof argsOrUrl == "string"){ |
| 46 | argsOrUrl = {url: argsOrUrl}; |
| 47 | } |
| 48 | return WebSocket ? Socket.WebSocket(argsOrUrl, true) : Socket.LongPoll(argsOrUrl); |
| 49 | }; |
| 50 | |
| 51 | Socket.WebSocket = function(args, fallback){ |
| 52 | // summary: |
| 53 | // A wrapper for WebSocket, than handles standard args and relative URLs |
| 54 | var baseURI = document.baseURI || window.location.href; |
| 55 | var ws = new WebSocket(new dBaseUrl(baseURI.replace(/^http/i,'ws'), args.url)); |
| 56 | ws.on = function(type, listener){ |
| 57 | ws.addEventListener(type, listener, true); |
| 58 | }; |
| 59 | var opened; |
| 60 | aspect.after(ws, "onopen", function(event){ |
| 61 | opened = true; |
| 62 | }, true); |
| 63 | aspect.after(ws, "onclose", function(event){ |
| 64 | if(opened){ |
| 65 | return; |
| 66 | } |
| 67 | if(fallback){ |
| 68 | Socket.replace(ws, Socket.LongPoll(args), true); |
| 69 | } |
| 70 | }, true); |
| 71 | return ws; |
| 72 | }; |
| 73 | |
| 74 | Socket.replace = function(socket, newSocket, listenForOpen){ |
| 75 | // make the original socket a proxy for the new socket |
| 76 | socket.send = lang.hitch(newSocket, "send"); |
| 77 | socket.close = lang.hitch(newSocket, "close"); |
| 78 | var proxyEvent = function(type){ |
| 79 | (newSocket.addEventListener || newSocket.on).call(newSocket, type, function(event){ |
| 80 | on.emit(socket, event.type, event); |
| 81 | }, true); |
| 82 | }; |
| 83 | |
| 84 | if(listenForOpen){ |
| 85 | proxyEvent("open"); |
| 86 | } |
| 87 | // redirect the events as well |
| 88 | array.forEach(["message", "close", "error"], proxyEvent); |
| 89 | }; |
| 90 | |
| 91 | Socket.LongPoll = function(/*dojo.__XhrArgs*/ args){ |
| 92 | // summary: |
| 93 | // Provides a simple long-poll based comet-style socket/connection to a server and returns an |
| 94 | // object implementing the WebSocket interface: |
| 95 | // http://dev.w3.org/html5/websockets/#websocket |
| 96 | // args: |
| 97 | // This uses the same arguments as the other I/O functions in Dojo, with this addition: |
| 98 | // args.interval: |
| 99 | // Indicates the amount of time (in milliseconds) after a response was received |
| 100 | // before another request is made. By default, a request is made immediately |
| 101 | // after getting a response. The interval can be increased to reduce load on the |
| 102 | // server or to do simple time-based polling where the server always responds |
| 103 | // immediately. |
| 104 | // args.transport: |
| 105 | // Provide an alternate transport like dojo.io.script.get |
| 106 | // returns: |
| 107 | // An object that implements the WebSocket API |
| 108 | // example: |
| 109 | // | dojo.require("dojox.socket.LongPoll"); |
| 110 | // | var socket = dojox.socket.LongPoll({url:"/comet"}); |
| 111 | // or: |
| 112 | // | dojo.require("dojox.socket.LongPoll"); |
| 113 | // | dojox.socket.LongPoll.add(); |
| 114 | // | var socket = dojox.socket({url:"/comet"}); |
| 115 | |
| 116 | var cancelled = false, |
| 117 | first = true, |
| 118 | timeoutId, |
| 119 | connections = []; |
| 120 | |
| 121 | // create the socket object |
| 122 | var fire, connect; |
| 123 | var socket = { |
| 124 | send: function(data){ |
| 125 | // summary: |
| 126 | // Send some data using XHR or provided transport |
| 127 | var sendArgs = lang.delegate(args); |
| 128 | sendArgs.rawBody = data; |
| 129 | clearTimeout(timeoutId); |
| 130 | var deferred = first ? (first = false) || socket.firstRequest(sendArgs) : |
| 131 | socket.transport(sendArgs); |
| 132 | connections.push(deferred); |
| 133 | deferred.then(function(response){ |
| 134 | // got a response |
| 135 | socket.readyState = 1; |
| 136 | // remove the current connection |
| 137 | connections.splice(array.indexOf(connections, deferred), 1); |
| 138 | // reconnect to listen for the next message if there are no active connections, |
| 139 | // we queue it up in case one of the onmessage handlers has a message to send |
| 140 | if(!connections.length){ |
| 141 | timeoutId = setTimeout(connect, args.interval); |
| 142 | } |
| 143 | if(response){ |
| 144 | // now send the message along to listeners |
| 145 | fire("message", {data: response}, deferred); |
| 146 | } |
| 147 | }, function(error){ |
| 148 | connections.splice(array.indexOf(connections, deferred), 1); |
| 149 | // an error occurred, fire the appropriate event listeners |
| 150 | if(!cancelled){ |
| 151 | fire("error", {error:error}, deferred); |
| 152 | if(!connections.length){ |
| 153 | clearTimeout(timeoutId); |
| 154 | socket.readyState = 3; |
| 155 | fire("close", {wasClean:false}, deferred); |
| 156 | } |
| 157 | } |
| 158 | }); |
| 159 | return deferred; |
| 160 | }, |
| 161 | close: function(){ |
| 162 | // summary: |
| 163 | // Close the connection |
| 164 | socket.readyState = 2; |
| 165 | cancelled = true; |
| 166 | var i; |
| 167 | for(i = 0; i < connections.length; i++){ |
| 168 | connections[i].cancel(); |
| 169 | } |
| 170 | socket.readyState = 3; |
| 171 | fire("close", {wasClean:true}); |
| 172 | }, |
| 173 | transport: args.transport || xhr.post, |
| 174 | args: args, |
| 175 | url: args.url, |
| 176 | readyState: 0, |
| 177 | CONNECTING: 0, |
| 178 | OPEN: 1, |
| 179 | CLOSING: 2, |
| 180 | CLOSED: 3, |
| 181 | on: Evented.prototype.on, |
| 182 | firstRequest: function(args){ |
| 183 | // summary: |
| 184 | // This allows for special handling for the first request. This is useful for |
| 185 | // providing information to disambiguate between the first request and |
| 186 | // subsequent long-poll requests so the server can properly setup a |
| 187 | // connection on the first connection or reject a request for an expired |
| 188 | // connection if the request is not expecting to be the first for a connection. |
| 189 | // This method can be overriden. The default behavior is to include a Pragma |
| 190 | // header with a value of "start-long-poll" |
| 191 | var headers = (args.headers || (args.headers = {})); |
| 192 | headers.Pragma = "start-long-poll"; |
| 193 | try{ |
| 194 | return this.transport(args); |
| 195 | }finally{ |
| 196 | // cleanup the header so it is not used on subsequent requests |
| 197 | delete headers.Pragma; |
| 198 | } |
| 199 | } |
| 200 | }; |
| 201 | fire = function(type, object, deferred){ |
| 202 | if(socket["on" + type]){ |
| 203 | object.ioArgs = deferred && deferred.ioArgs; |
| 204 | object.type = type; |
| 205 | on.emit(socket, type, object); |
| 206 | } |
| 207 | }; |
| 208 | connect = function(){ |
| 209 | if(socket.readyState === 0){ |
| 210 | // we fire the open event now because we really don't know when the "socket" |
| 211 | // is truly open, and this gives us a to do a send() and get it included in the |
| 212 | // HTTP request |
| 213 | fire("open",{}); |
| 214 | } |
| 215 | // make the long-poll connection, to wait for response from the server |
| 216 | if(!connections.length){ |
| 217 | socket.send(); |
| 218 | } |
| 219 | }; |
| 220 | // provide an alias for Dojo's connect method |
| 221 | socket.connect = socket.on; |
| 222 | // do the initial connection |
| 223 | setTimeout(connect); |
| 224 | return socket; |
| 225 | }; |
| 226 | |
| 227 | return Socket; |
| 228 | |
| 229 | }); |