4 * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
8 (function (exports, io, global) {
14 exports.websocket = WS;
17 * The WebSocket transport uses the HTML5 WebSocket API to establish an
18 * persistent connection with the Socket.IO server. This transport will also
19 * be inherited by the FlashSocket fallback as it provides a API compatible
20 * polyfill for the WebSockets.
23 * @extends {io.Transport}
27 function WS (socket) {
28 io.Transport.apply(this, arguments);
32 * Inherits from Transport.
35 io.util.inherit(WS, io.Transport);
43 WS.prototype.name = 'websocket';
46 * Initializes a new `WebSocket` connection with the Socket.IO server. We attach
47 * all the appropriate listeners to handle the responses from the server.
49 * @returns {Transport}
53 WS.prototype.open = function () {
54 var query = io.util.query(this.socket.options.query)
59 Socket = require('ws');
63 Socket = global.MozWebSocket || global.WebSocket;
66 this.websocket = new Socket(this.prepareUrl() + query);
68 this.websocket.onopen = function () {
70 self.socket.setBuffer(false);
72 this.websocket.onmessage = function (ev) {
75 this.websocket.onclose = function () {
77 self.socket.setBuffer(true);
79 this.websocket.onerror = function (e) {
87 * Send a message to the Socket.IO server. The message will automatically be
88 * encoded in the correct message format.
90 * @returns {Transport}
94 // Do to a bug in the current IDevices browser, we need to wrap the send in a
95 // setTimeout, when they resume from sleeping the browser will crash if
96 // we don't allow the browser time to detect the socket has been closed
97 if (io.util.ua.iDevice) {
98 WS.prototype.send = function (data) {
100 setTimeout(function() {
101 self.websocket.send(data);
106 WS.prototype.send = function (data) {
107 this.websocket.send(data);
118 WS.prototype.payload = function (arr) {
119 for (var i = 0, l = arr.length; i < l; i++) {
126 * Disconnect the established `WebSocket` connection.
128 * @returns {Transport}
132 WS.prototype.close = function () {
133 this.websocket.close();
138 * Handle the errors that `WebSocket` might be giving when we
139 * are attempting to connect or send messages.
141 * @param {Error} e The error.
145 WS.prototype.onError = function (e) {
146 this.socket.onError(e);
150 * Returns the appropriate scheme for the URI generation.
154 WS.prototype.scheme = function () {
155 return this.socket.options.secure ? 'wss' : 'ws';
159 * Checks if the browser has support for native `WebSockets` and that
160 * it's not the polyfill created for the FlashSocket transport.
166 WS.check = function () {
170 return ('WebSocket' in global && !('__addTask' in WebSocket))
171 || 'MozWebSocket' in global;
175 * Check if the `WebSocket` transport support cross domain communications.
181 WS.xdomainCheck = function () {
186 * Add the transport to your public io.transports array.
191 io.transports.push('websocket');
194 'undefined' != typeof io ? io.Transport : module.exports
195 , 'undefined' != typeof io ? io : module.parent.exports