Files
P42_wssGateway/wssServer.js
T
2025-10-07 16:11:17 +00:00

108 lines
3.9 KiB
JavaScript

import {AccesRights} from './accesRights.js'
import crypto from 'crypto'
import {WssConnexion} from './wssConnexion.js'
export class wssServer {
constructor(configHelper, WSSServer, allRediscnx, debug) {
this.debug = debug
if(this.debug) console.log('Starting WSSGateway...')
this.allRediscnx = allRediscnx
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();
if(socket.session && socket.session.authenticated && socket.session.userInfos && socket.session.userInfos.identity && socket.session.userInfos.identity.username){
var wssCnx = new WssConnexion({
socket: socket,
req, req,
uuid: uuid,
wssSrv: this,
debug: this.debug,
config: this.wssGatewayConfig,
allRediscnx: this.allRediscnx,
accessRights: this.accessRights,
userId: socket.session.userInfos.identity.uuid,
roles: socket.session.userInfos.roles,
});
this.AllWssConnections[uuid] = wssCnx;
if(!(wssCnx.userId in this.Users2uuids)) this.Users2uuids[wssCnx.userId] = new Set();
this.Users2uuids[wssCnx.userId].add(uuid);
this.OnlineUsers.add(wssCnx.userId);
this.allRediscnx.forEach(cnx => { cnx.wssConnections[uuid] = wssCnx })
this.postLoginActions(wssCnx)
} else socket.close()
}
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);
}
this.allRediscnx.forEach(cnx => delete cnx.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)
}
}