2nd
This commit is contained in:
+110
@@ -0,0 +1,110 @@
|
||||
const AccesRights = require('./accesRights')
|
||||
const crypto = require('crypto')
|
||||
const WssConnexion = require('./wssConnexion')
|
||||
|
||||
module.exports = class wssServer {
|
||||
|
||||
constructor(configHelper, WSSServer, REDIScnx, debug) {
|
||||
this.debug = debug
|
||||
if(this.debug) console.log('Starting WSSGateway...')
|
||||
this.REDIScnx = REDIScnx
|
||||
this.configHelper = configHelper
|
||||
this.wssGatewayConfig = configHelper.config
|
||||
this.AllWssConnections = {}
|
||||
this.Users2uuids = {}
|
||||
this.OnlineUsers = new Set()
|
||||
this.accessRights = new AccesRights(configHelper.config, this.debug)
|
||||
|
||||
WSSServer.on('error', (err) => {
|
||||
console.warn('wssGateway websocket error:', error)
|
||||
});
|
||||
|
||||
WSSServer.on('listening', (e) => {
|
||||
console.log('wssGateway listening for websockets on ' + configHelper.config.server.listenPort)
|
||||
});
|
||||
|
||||
WSSServer.on('connection', this.newWSSConnexion.bind(this))
|
||||
|
||||
if(this.debug) console.log('WSS Gateway ready...')
|
||||
}
|
||||
|
||||
newWSSConnexion(socket, req) {
|
||||
var uuid = crypto.randomUUID();
|
||||
var wssCnx = new WssConnexion({
|
||||
socket: socket,
|
||||
req, req,
|
||||
uuid: uuid,
|
||||
wssSrv: this,
|
||||
debug: this.debug,
|
||||
config: this.wssGatewayConfig,
|
||||
rediscnx: this.REDIScnx,
|
||||
accessRights: this.accessRights,
|
||||
});
|
||||
this.AllWssConnections[uuid] = wssCnx;
|
||||
wssCnx.doLogin().then(() => { // Things to execute only when successfuly logged-in
|
||||
if(!(wssCnx.userId in this.Users2uuids)) this.Users2uuids[wssCnx.userId] = new Set();
|
||||
this.Users2uuids[wssCnx.userId].add(uuid);
|
||||
this.OnlineUsers.add(wssCnx.userId);
|
||||
this.REDIScnx.wssConnections[uuid] = wssCnx;
|
||||
//}).then(() => {
|
||||
wssCnx.send(JSON.stringify({
|
||||
'action': 'LOGIN',
|
||||
'logged': true
|
||||
}))
|
||||
this.postLoginActions(wssCnx)
|
||||
})
|
||||
}
|
||||
|
||||
postLoginActions(wssCnx) {
|
||||
wssCnx.startKeepAlive()
|
||||
wssCnx.subscribeMandatoryChans()
|
||||
wssCnx.action_SUBLST('SUBLST', null, '')
|
||||
this.fanoutOnlineUsers(this.getOnlineUsers());
|
||||
}
|
||||
|
||||
cleanupConnexion(uuid, userId) {
|
||||
delete(this.AllWssConnections[uuid]);
|
||||
if(userId in this.Users2uuids) {
|
||||
this.Users2uuids[userId].delete(uuid);
|
||||
if(this.Users2uuids[userId].size == 0) this.OnlineUsers.delete(userId);
|
||||
}
|
||||
delete(this.REDIScnx.wssConnections[uuid]);
|
||||
this.fanoutOnlineUsers(this.getOnlineUsers());
|
||||
}
|
||||
|
||||
fanoutOnlineUsers(onlineUsers) {
|
||||
for(let uuid in this.AllWssConnections) {
|
||||
// Normally should not happen as you're added only after login.(newWSSConnexion)
|
||||
if(this.AllWssConnections.cnxState!='CONNECTED') continue
|
||||
this.AllWssConnections[uuid].updateOnlineUsers(onlineUsers);
|
||||
}
|
||||
}
|
||||
|
||||
userConnected(uid){
|
||||
return( this.OnlineUsers.has(uid))
|
||||
}
|
||||
|
||||
getOnlineUsers() {
|
||||
var OnlineUsers = {};
|
||||
for(var usr of this.OnlineUsers.values()) {
|
||||
if(usr in this.Users2uuids) {
|
||||
OnlineUsers[usr] = this.Users2uuids[usr].size;
|
||||
}
|
||||
}
|
||||
return(OnlineUsers);
|
||||
}
|
||||
|
||||
sessionConnected(sessionID){
|
||||
if(!sessionID) return(false) // If that cnx is not finished login-in
|
||||
for(let uuid in this.AllWssConnections) {
|
||||
if(this.AllWssConnections[uuid].sessionID==sessionID) return(true)
|
||||
}
|
||||
return(false)
|
||||
}
|
||||
|
||||
async reloadAccessRights() {
|
||||
await this.configHelper.refreshAccessRights()
|
||||
this.wssGatewayConfig.accessRights = this.configHelper.config.accessRights
|
||||
this.accessRights.refreshAccessRights(this.wssGatewayConfig)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user