
Basically the peer map system that we had before isn't going to work simply because succesful client connections already have the data we need inside them. Adding a whole wrapper around this just complicates things in a way that doesn't really give any benefit. For now every message is echo'd to all connections but that is easy enough to change
45 lines
1005 B
JavaScript
45 lines
1005 B
JavaScript
const ws = require('ws')
|
|
const auth = require('./auth.js')
|
|
|
|
const server = new ws.Server({
|
|
port: 5648,
|
|
clientTracking: true
|
|
})
|
|
|
|
server.on('connection', function(socket, req) {
|
|
let [jwt, path] = auth.prepare_auth(req)
|
|
let conn = auth.verify(jwt)
|
|
if(conn == 'server') {
|
|
console.log('[WSS] New server connection')
|
|
|
|
socket.on('close', function() {
|
|
console.log('[WSS] Server connection closed')
|
|
})
|
|
|
|
socket.on('message', function(message) {
|
|
// do some parsing, then emit to everyone
|
|
server.clients.forEach(client => {
|
|
if (client !== socket && client.readyState === ws.OPEN) {
|
|
client.send(message)
|
|
}
|
|
})
|
|
})
|
|
|
|
} else if(conn == 'user') {
|
|
socket.on('message', function(message) {
|
|
// some kind of parsing here
|
|
// no actual emitting to all users
|
|
server.clients.forEach(client => {
|
|
if (client !== socket && client.readyState === ws.OPEN) {
|
|
client.send(message)
|
|
}
|
|
})
|
|
})
|
|
|
|
} else {
|
|
console.log('[WSS] No valid auth', conn)
|
|
socket.close()
|
|
}
|
|
|
|
})
|