glowingbear-mainbox/js/websockets.js

145 lines
3.2 KiB
JavaScript
Raw Normal View History

2014-02-08 16:46:58 +01:00
var websockets = angular.module('ngWebsockets', []);
2014-02-09 18:06:04 +01:00
websockets.factory('ngWebsockets',
2014-02-08 16:46:58 +01:00
['$rootScope','$q',
function($rootScope, $q) {
2014-02-24 11:28:43 +01:00
2014-02-09 18:06:04 +01:00
this.protocol = null;
2014-02-24 11:28:43 +01:00
2014-02-08 16:46:58 +01:00
var ws = null;
var callbacks = {};
var currentCallBackId = 0;
/*
* Fails every currently subscribed callback for the
* given reason
*
* @param reason reason for failure
*/
failCallbacks = function(reason) {
for (var i in callbacks) {
callbacks[i].cb.reject(reason);
}
};
2014-02-24 11:28:43 +01:00
2014-02-08 16:46:58 +01:00
/*
* Returns the current callback id
*/
var getCurrentCallBackId = function() {
currentCallBackId += 1;
if (currentCallBackId > 1000) {
currentCallBackId = 0;
}
return currentCallBackId;
};
/* Send a message to the websocket and returns a promise.
* See: http://docs.angularjs.org/api/ng.$q
*
* @param message message to send
* @returns a promise
*/
var send = function(message) {
var cb = createCallback(message);
message = protocol.setId(cb.id,
message);
ws.send(message);
return cb.promise;
};
/*
* Create a callback, adds it to the callback list
* and return it.
*/
var createCallback = function() {
var defer = $q.defer();
var cbId = getCurrentCallBackId();
callbacks[cbId] = {
time: new Date(),
cb: defer
};
defer.id = cbId;
return defer;
};
/*
* Send all messages to the websocket and returns a promise that is resolved
* when all message are resolved.
*
* @param messages list of messages
* @returns a promise
*/
var sendAll = function(messages) {
var promises = [];
for (var i in messages) {
var promise = send(messages[i]);
promises.push(promise);
}
return $q.all(promises);
};
var onmessage = function (evt) {
/*
* Receives a message on the websocket
*/
2014-02-10 01:33:35 +01:00
var message = protocol.parse(evt.data);
2014-02-08 16:46:58 +01:00
if (_.has(callbacks, message.id)) {
// see if it's bound to one of the callbacks
var promise = callbacks[message.id];
2014-02-10 01:33:35 +01:00
promise.cb.resolve(message);
2014-02-08 16:46:58 +01:00
delete(callbacks[message.id]);
} else {
// otherwise emit it
2014-02-10 01:33:35 +01:00
$rootScope.$emit('onMessage', message);
2014-02-08 16:46:58 +01:00
}
2014-02-08 16:46:58 +01:00
$rootScope.$apply();
2014-02-10 01:33:35 +01:00
};
2014-02-08 16:46:58 +01:00
var connect = function(url,
protocol,
properties) {
ws = new WebSocket(url);
protocol = protocol;
for (var property in properties) {
ws[property] = properties[property];
}
if ('onmessage' in properties) {
ws.onmessage = function(event) {
2014-02-14 15:32:30 +01:00
properties.onmessage(event);
onmessage(event);
2014-02-14 15:32:30 +01:00
};
} else {
ws.onmessage = onmessage;
}
2014-02-10 01:33:35 +01:00
};
2014-02-08 16:46:58 +01:00
var disconnect = function() {
ws.close();
2014-02-10 01:33:35 +01:00
};
2014-02-08 16:46:58 +01:00
return {
send: send,
sendAll: sendAll,
connect: connect,
disconnect: disconnect
2014-02-10 01:33:35 +01:00
};
2014-02-08 16:46:58 +01:00
}]);