mirror of
https://github.com/EQEmu/Server.git
synced 2026-09-04 22:56:37 +00:00
Basic work on subscriptions
This commit is contained in:
+20
-1
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user