dojox/socket (version 1.10)

Summary

Provides a simple socket connection using WebSocket, or alternate communication mechanisms 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

Provides socket connections. This can be used with virtually any Comet protocol.

Usage

socket(argsOrUrl);
Parameter Type Description
argsOrUrl Object

This uses the same arguments as the other I/O functions in Dojo, or a URL to connect to. The URL should be a relative URL in order to properly work with WebSockets (it can still be host relative, like //other-site.org/endpoint)

Returns:any | undefined

An object that implements the WebSocket API

Examples

Example 1

dojo.require("dojox.socket");
var socket = dojox.socket({"url://comet-server/comet");
// we could also add auto-reconnect support
// now we can connect to standard HTML5 WebSocket-style events
dojo.connect(socket, "onmessage", function(event){
   var message = event.data;
   // do something with the message
});
// send something
socket.send("hi there");
whenDone(function(){
  socket.close();
});

You can also use the Reconnect module:

dojo.require("dojox.socket");
dojo.require("dojox.socket.Reconnect");
var socket = dojox.socket({url:"/comet"});
// add auto-reconnect support
socket = dojox.socket.Reconnect(socket);

Method Summary

  • LongPoll(args) Provides a simple long-poll based comet-style socket/connection to a server and returns an object implementing the WebSocket interface: http://dev.w3.org/html5/websockets/#websocket
  • replace(socket,newSocket,listenForOpen)
  • WebSocket(args,fallback) A wrapper for WebSocket, than handles standard args and relative URLs

Methods

LongPoll(args)
Defined by dojox/socket

Provides a simple long-poll based comet-style socket/connection to a server and returns an object implementing the WebSocket interface: http://dev.w3.org/html5/websockets/#websocket

Parameter Type Description
args Object

This uses the same arguments as the other I/O functions in Dojo, with this addition: args.interval: Indicates the amount of time (in milliseconds) after a response was received before another request is made. By default, a request is made immediately after getting a response. The interval can be increased to reduce load on the server or to do simple time-based polling where the server always responds immediately. args.transport: Provide an alternate transport like dojo.io.script.get

Returns:any

An object that implements the WebSocket API

Examples

Example 1

dojo.require("dojox.socket.LongPoll");
var socket = dojox.socket.LongPoll({url:"/comet"});

or:

dojo.require("dojox.socket.LongPoll");
dojox.socket.LongPoll.add();
var socket = dojox.socket({url:"/comet"});
replace(socket,newSocket,listenForOpen)
Defined by dojox/socket
Parameter Type Description
socket undefined
newSocket undefined
listenForOpen undefined
WebSocket(args,fallback)
Defined by dojox/socket

A wrapper for WebSocket, than handles standard args and relative URLs

Parameter Type Description
args undefined
fallback undefined
Error in the documentation? Can’t find what you are looking for? Let us know!