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.
33 lines
575 B
33 lines
575 B
import * as WebSocket from 'ws'
|
|
|
|
let connections = new Map()
|
|
let connectionCounter = 0
|
|
|
|
const connect = (server) => {
|
|
server = new WebSocket.Server({ server })
|
|
|
|
console.log('[socket] connected to server')
|
|
|
|
server.on('connection', socket => {
|
|
console.log('[socket] connection received')
|
|
var id = connectionCounter++;
|
|
connections.set(id, socket)
|
|
})
|
|
|
|
}
|
|
|
|
const send = (body) => {
|
|
connections.forEach((socket, index) => {
|
|
socket.send(body)
|
|
})
|
|
}
|
|
|
|
const sendJson = (object) => {
|
|
send(JSON.stringify(object))
|
|
}
|
|
|
|
export {
|
|
connect,
|
|
send,
|
|
sendJson
|
|
}
|
|
|