Change to use Socket.IO

This commit is contained in:
wancat committed 2023-07-15 12:29:54 +08:00
1 parent c80a6fae6d
commit f17c2a9ef5
4 files changed
+148 -20

No files matched your search

+17 -5
View File
@@ -1,13 +1,25 @@
import { WebSocketServer } from 'ws'
import express from 'express'
import http from 'http'
import { Server } from 'socket.io'
const wss = new WebSocketServer({ port: 8080 })
const app = express()
const server = http.createServer(app)
const io = new Server(server)
wss.on('connection', function connection(ws) {
io.on('connection', (socket) => {
console.log('Connected to the client.')
ws.on('message', function incoming(message) {
socket.on('message', function incoming(message) {
console.log(`From client: ${message}`)
})
ws.send('Hello from server!')
socket.on('close', () => {
console.log('Disconnected from the client.')
})
socket.send('Hello from server!')
})
server.listen(8080, () => {
console.log(`Server listening at http://localhost:8080`)
})