diff --git a/server/client.js b/server/client.js new file mode 100644 index 0000000..43d766c --- /dev/null +++ b/server/client.js @@ -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) +}) diff --git a/server/websocket.js b/server/websocket.js new file mode 100644 index 0000000..2bcb0f5 --- /dev/null +++ b/server/websocket.js @@ -0,0 +1,13 @@ +import { WebSocketServer } from 'ws' + +const wss = new WebSocketServer({ port: 8080 }) + +wss.on('connection', function connection(ws) { + console.log('Connected to the client.') + + ws.on('message', function incoming(message) { + console.log(`From client: ${message}`) + }) + + ws.send('Hello from server!') +})