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.
|
|
import { WebSocketServer } from 'ws'
export const clients = new Set()
export const broadcast = (message) => { try { const jsonMessage = JSON.stringify(message) clients.forEach((client) => { if (client.readyState === WebSocket.OPEN) { client.send(jsonMessage) } }) } catch (e) { console.log('广播发送失败:', e, message) } }
export const initWebSocketServer = () => { const wss = new WebSocketServer({ port: 9527 })
wss.on('connection', (ws) => { clients.add(ws) ws.on('close', () => { console.log('客户端断开') }) }) }
|