metajack/strophejs — pull request #95 by Gordin (2012-09-04 03:35:10+00:00)
Title: Websocket Support

Like the title says, I implemented WebSocket support in StropheJS.
By Websocket support I mean the latest draft-moffitt-xmpp-over-websocket-01 from here: http://tools.ietf.org/html/draft-moffitt-xmpp-over-websocket-01

What I''ve done specifically:
core.js is now split up into three different files:
* websocket.js contains code that is only needed if you want to use Websockets.
* bosh.js contains code that is only needed if you want to use BOSH.
* core.js contains code that is needed for both protocols. (with the exception of "attach", which only works with BOSH)

While implementing support for Websockets I tried to leave as much existing code unchanged as possible to still be able to merge patches from other branches that do not suppert Websockets (or at least make it very easy to merge these manually). Also I tried to make the Websocket code use as much of the already existing handlers as possible. What I basically did is wrap stanzas in an empty

tag and then (where possible) put it through the same handlers that are used for all types of connections (which currently if of course only BOSH). The framework that StropheJS uses is mostly unchanged, many functions are just split up in a part that is needed for both protocols and a part that depends on the current protocol object. In theory implementing other new protocols would be possible without touching core.js (Not that there are much other protocols one could use...).
Also, this branch is API-compatible to the current version of StropheJS.
Wether Websockets or BOSH is used is determined by the URL you create a connection with (URLs starting with ws:// will use Websocket protocol, everything else will fall back to BOSH). This can be decided on a per-connection level and BOSH and Websocket connection can both be open at the same time.
Of course trying to "attach" a Websocket connection will fail as there is no real equivalent for what you would do in BOSH, I just left that in core to not brake the API (yet).

I tested this branch against websocket plugins for ejabberd and Prosody and with the Websocket-over-XMPP proxies node-xmpp-bosh and WXG and it seems to be stable.
Everything I added is also fully commented and the code of websocket.js and bosh.js is structured the same way for easy comparison.

I'd really like to have this merged into StropheJS, so if there are any issues with this branch that need fixing I will of course do that.