Cleaned up action naming and handling

This commit is contained in:
khlieng 2015-01-22 00:14:28 +01:00
parent e1cb8c468d
commit 43a725c662
9 changed files with 129 additions and 139 deletions

View file

@ -2,40 +2,28 @@ var EventEmitter = require('events').EventEmitter;
var _ = require('lodash');
var sockets = {};
function createSocket(path) {
if (sockets[path]) {
return sockets[path];
} else {
var ws = new WebSocket('ws://' + window.location.host + path);
var sock = {
send: function(type, data) {
ws.send(JSON.stringify({ type: type, request: data }));
}
};
_.extend(sock, EventEmitter.prototype);
sockets[path] = sock;
ws.onopen = function() {
sock.emit('connect');
};
ws.onclose = function() {
sock.emit('disconnect');
};
ws.onmessage = function(e) {
var msg = JSON.parse(e.data);
sock.emit(msg.type, msg.response);
};
return sock;
var socket = {
send: function(type, data) {
ws.send(JSON.stringify({ type: type, request: data }));
}
}
};
module.exports = createSocket;
_.extend(socket, EventEmitter.prototype);
var ws = new WebSocket('ws://' + window.location.host + '/ws');
ws.onopen = function() {
socket.emit('connect');
};
ws.onclose = function() {
socket.emit('disconnect');
};
ws.onmessage = function(e) {
var msg = JSON.parse(e.data);
socket.emit(msg.type, msg.response);
};
module.exports = socket;