Basic work on subscriptions

This commit is contained in:
KimLS
2017-01-13 21:52:08 -08:00
parent f24770489e
commit 1cafd6831d
12 changed files with 196 additions and 42 deletions
+20 -1
View File
@@ -1,18 +1,21 @@
const WebSocketServer = require('ws').Server;
const jwt = require('jsonwebtoken');
const uuid = require('node-uuid');
class WebSocketInterface
{
constructor(server, key) {
constructor(server, key, api) {
this.wss = new WebSocketServer({ server: server });
this.methods = {};
var self = this;
this.wss.on('connection', function connection(ws) {
self.ws = ws;
ws.uuid = uuid.v4();
ws.on('message', function incoming(message) {
try {
var request = JSON.parse(message);
request.ws = ws;
if(request.method) {
var method = self.methods[request.method];
@@ -51,6 +54,14 @@ class WebSocketInterface
self.SendError(null, 'No method supplied');
}
});
ws.on('close', function() {
api.UnsubscribeAll(ws);
});
ws.on('subscriptionMessage', function(msg) {
self.SendRaw(msg);
});
});
}
@@ -92,6 +103,14 @@ class WebSocketInterface
console.log(ex);
}
}
SendRaw(obj) {
try {
this.ws.send(JSON.stringify(obj));
} catch(ex) {
console.log(ex);
}
}
}
module.exports = {