消毒机设备
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.

28 lines
601 B

  1. import { WebSocketServer } from 'ws'
  2. export const clients = new Set()
  3. export const broadcast = (message) => {
  4. try {
  5. const jsonMessage = JSON.stringify(message)
  6. clients.forEach((client) => {
  7. if (client.readyState === WebSocket.OPEN) {
  8. client.send(jsonMessage)
  9. }
  10. })
  11. }
  12. catch (e) {
  13. console.log('广播发送失败:', e, message)
  14. }
  15. }
  16. export const initWebSocketServer = () => {
  17. const wss = new WebSocketServer({ port: 9527 })
  18. wss.on('connection', (ws) => {
  19. clients.add(ws)
  20. ws.on('close', () => {
  21. console.log('客户端断开')
  22. })
  23. })
  24. }