mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
Change to use Socket.IO
This commit is contained in:
1 parent
c80a6fae6d
commit
f17c2a9ef5
4 files changed
+148
-20
No files matched your search
+17
-5
@@ -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`)
|
||||
})
|
||||
Reference in new issue
Block a user