Create a websocket server and client

This commit is contained in:
wancat committed 2023-07-15 11:55:46 +08:00
1 parent 0ca4bf0bc8
commit c80a6fae6d
2 files changed
+40

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
import WebSocket from 'ws'
import readline from 'readline'
import { exit } from 'process'
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
const ws = new WebSocket('ws://localhost:8080')
ws.on('open', function open() {
console.log('Connected to the server.')
})
ws.on('message', function incoming(data) {
console.log(`From server: ${data}`)
})
ws.on('close', function close() {
console.log('Disconnected from the server.')
exit()
})
rl.on('line', (input) => {
ws.send(input)
})