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
+6
-6
@@ -1,27 +1,27 @@
|
||||
import WebSocket from 'ws'
|
||||
import readline from 'readline'
|
||||
import { exit } from 'process'
|
||||
import { io } from 'socket.io-client'
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
})
|
||||
|
||||
const ws = new WebSocket('ws://localhost:8080')
|
||||
const socket = io('ws://localhost:8080')
|
||||
|
||||
ws.on('open', function open() {
|
||||
socket.on('open', function open() {
|
||||
console.log('Connected to the server.')
|
||||
})
|
||||
|
||||
ws.on('message', function incoming(data) {
|
||||
socket.on('message', function incoming(data) {
|
||||
console.log(`From server: ${data}`)
|
||||
})
|
||||
|
||||
ws.on('close', function close() {
|
||||
socket.on('close', function close() {
|
||||
console.log('Disconnected from the server.')
|
||||
exit()
|
||||
})
|
||||
|
||||
rl.on('line', (input) => {
|
||||
ws.send(input)
|
||||
socket.send(input)
|
||||
})
|
||||
+2
-1
@@ -10,7 +10,8 @@
|
||||
"dependencies": {
|
||||
"dockerode": "^3.3.5",
|
||||
"express": "^4.18.2",
|
||||
"ws": "^8.13.0"
|
||||
"socket.io": "^4.7.1",
|
||||
"socket.io-client": "^4.7.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^3.0.1"
|
||||
|
||||
+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