Skip to content

Commit

Permalink
#172 better socket.io init.
Browse files Browse the repository at this point in the history
  • Loading branch information
itayw committed Jun 19, 2015
1 parent 86f41fa commit 9d192e7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/lib/common/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,17 @@ api.getJSON = function (options, objOptions, callback) {

objOptions._path = options.path;

joola.io.socket.emit(routeID, objOptions);
joola.io.emit(routeID, objOptions);
joola.events.emit('rpc:start', 1);
joola.events.emit('bandwidth', lengthInUtf8Bytes(JSON.stringify(objOptions)));
var shouldOn = objOptions && (objOptions.realtime || (objOptions.options && Array.isArray(objOptions.options) && objOptions.options.length > 0 && objOptions.options[0].realtime));
if (objOptions && objOptions.options && objOptions.options.realtime)
shouldOn = true;
if (shouldOn) {
joola.io.socket.on(routeID + ':done', processResponse);
joola.io.on(routeID + ':done', processResponse);
}
else {
joola.io.socket.once(routeID + ':done', processResponse);
joola.io.once(routeID + ':done', processResponse);
}
}
};
Expand Down
17 changes: 9 additions & 8 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,31 +172,32 @@ joola.init = function (options, callback) {
throw new Error('joola host not specified');

var io = require('socket.io-client');
joola.io = io;
joola.io.socket = joola.io.connect(joola.options.host);
joola.io.socket.on('event', function (data) {
joola.io = io.connect(joola.options.host, {'force new connection': true });
joola.io.on('error', function (reason) {
joola.connected = false;
joola.emit('disconnected', reason);
});
joola.io.socket.on('disconnect', function (reason) {
joola.io.on('disconnect', function (reason) {
joola.connected = false;
joola.emit('disconnected', reason);
});
joola.io.socket.on('connect_error', function (err) {
joola.io.on('connect_error', function (err) {
joola.connected = false;
if (!joola.online)
throw new Error('Failed to connect to Joola engine: ' + err);
joola.emit('disconnected', err);
});
joola.io.socket.on('connect_timeout', function (err) {
joola.io.on('connect_timeout', function (err) {
joola.connected = false;
if (!joola.online)
throw new Error('Failed to connect to Joola engine: Timeout');
joola.emit('disconnected', 'timeout');
});
joola.io.socket.on('connect', function () {
joola.io.on('connect', function () {
joola.connected = true;
if (!joola.online) {
joola.online = true;
joola.bringOnline(callback);
joola.bringOnline(callback);
}
joola.emit('connected');
});
Expand Down

0 comments on commit 9d192e7

Please sign in to comment.