You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
802 B

5 years ago
import * as WebSocket from 'ws'
5 years ago
// let connections = new Map()
// let connectionCounter = 0
5 years ago
// let lastConn: any
5 years ago
let srvr: any
5 years ago
const connect = (server) => {
5 years ago
srvr = new WebSocket.Server({ server, clientTracking:true })
console.log('=> [socket] connected to server')
5 years ago
5 years ago
srvr.on('connection', socket => {
console.log('=> [socket] connection received')
5 years ago
// var id = connectionCounter++;
// connections.set(id, socket)
5 years ago
// lastConn = socket
5 years ago
})
}
const send = (body) => {
5 years ago
// connections.forEach((socket, index) => {
// socket.send(body)
// })
5 years ago
// if(lastConn) lastConn.send(body)
5 years ago
srvr.clients.forEach(c=>{
5 years ago
if(c && c.connected) {
c.send(body)
}
})
5 years ago
}
const sendJson = (object) => {
send(JSON.stringify(object))
}
export {
connect,
send,
sendJson
}