groovebasin/lib/web_socket_api_client.js
Andrew Kelley f2712ddaad re-write interface between client and server
* Use raw web sockets instead of socket.io. closes #114
 * promote most plugin code to be actually integrated with
   groovebasin, except last.fm
 * ability to upgrade from mpd protocol to groovebasin protocol
 * serialize/deserialize lastQueueDate and update it for all types of queuing.
   Fixes DynamicMode losing randomness data on server restart.
 * repeat state is saved between restarts
 * fixed a race condition in player.importFile
 * a smaller subset of db file properties are sent to the server, reducing
   library payload size
 * diffs are sent to client for playlist and library. closes #142
 * information is no longer requested in the groovebasin API; it is only
   subscribed to. closes #115
 * all commands go through the permissions framework. see #37
 * chat is deleted for now. closes #17
 * update to latest superagent and leveldb
 * fix stream activating and deactivating when seeking and not streaming
2014-03-03 05:25:50 -05:00

46 lines
1.1 KiB
JavaScript

var EventEmitter = require('events').EventEmitter;
var util = require('util');
module.exports = WebSocketApiClient;
util.inherits(WebSocketApiClient, EventEmitter);
function WebSocketApiClient(ws) {
EventEmitter.call(this);
this.ws = ws;
this.initialize();
}
WebSocketApiClient.prototype.sendMessage = function(name, args) {
this.ws.send(JSON.stringify({
name: name,
args: args,
}));
};
WebSocketApiClient.prototype.close = function() {
this.ws.close();
};
WebSocketApiClient.prototype.initialize = function() {
var self = this;
self.ws.on('message', function(data, flags) {
if (flags.binary) {
console.warn("ignoring binary web socket message");
return;
}
var msg;
try {
msg = JSON.parse(data);
} catch (err) {
console.warn("received invalid JSON from web socket:", err.message);
return;
}
self.emit('message', msg.name, msg.args);
});
self.ws.on('error', function(err) {
console.error("web socket error:", err.stack);
});
self.ws.on('close', function() {
self.emit('close');
});
};