mirror of
https://github.com/EQEmu/Server.git
synced 2026-09-06 04:06:38 +00:00
Basic work on subscriptions
This commit is contained in:
+5
-15
@@ -1,20 +1,10 @@
|
||||
function Register(name, wsi, api) {
|
||||
wsi.Register(name,
|
||||
function(request) {
|
||||
api.Call(name, request.params)
|
||||
.then(function(value) {
|
||||
wsi.Send(request, value);
|
||||
})
|
||||
.catch(function(reason) {
|
||||
wsi.SendError(request, reason);
|
||||
});
|
||||
}, true);
|
||||
}
|
||||
const common = require('./wi_common.js');
|
||||
|
||||
var RegisterEQW = function(wsi, api) {
|
||||
Register('EQW::IsLocked', wsi, api);
|
||||
Register('EQW::Lock', wsi, api);
|
||||
Register('EQW::Unlock', wsi, api);
|
||||
common.Register('EQW::IsLocked', wsi, api);
|
||||
common.Register('EQW::Lock', wsi, api);
|
||||
common.Register('EQW::Unlock', wsi, api);
|
||||
common.RegisterSubscription('EQW', 'WorldZoneUpdate', wsi, api);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
function Register(name, wsi, api) {
|
||||
wsi.Register(name,
|
||||
function(request) {
|
||||
api.Call(name, request.params)
|
||||
.then(function(value) {
|
||||
wsi.Send(request, value);
|
||||
})
|
||||
.catch(function(reason) {
|
||||
wsi.SendError(request, reason);
|
||||
});
|
||||
}, true);
|
||||
}
|
||||
|
||||
function RegisterSubscription(namespace, event, wsi, api) {
|
||||
wsi.Register(namespace + '::Subscribe::' + event, function(request) {
|
||||
api.Subscribe(event, request.ws);
|
||||
});
|
||||
|
||||
wsi.Register(namespace + '::Unsubscribe::' + event, function(request) {
|
||||
api.Unsubscribe(event, request.ws);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
'Register': Register,
|
||||
'RegisterSubscription': RegisterSubscription
|
||||
}
|
||||
+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