Sprinkle some ES6 on it

This commit is contained in:
khlieng 2015-02-21 07:48:25 +01:00
parent 518d3eaa07
commit fca2985d9b
4 changed files with 20 additions and 32 deletions

View file

@ -1,29 +1,21 @@
var EventEmitter = require('events').EventEmitter;
var _ = require('lodash');
class Socket extends EventEmitter {
constructor() {
this.ws = new WebSocket('ws://' + window.location.host + '/ws');
var ws = new WebSocket('ws://' + window.location.host + '/ws');
this.ws.onopen = () => this.emit('connect');
this.ws.onclose = () => this.emit('disconnect');
this.ws.onmessage = (e) => {
var msg = JSON.parse(e.data);
var socket = {
send: function(type, data) {
ws.send(JSON.stringify({ type: type, request: data }));
this.emit(msg.type, msg.response);
}
}
};
_.extend(socket, EventEmitter.prototype);
send(type, data) {
this.ws.send(JSON.stringify({ type: type, request: data }));
}
}
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;
module.exports = new Socket();