WebSocket echo for testing laptop tunnels
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
// WebSocket echo for testing deployshed's laptop tunnel: echoes each message
|
||||
// and pushes a tick every 5 s so a long-lived connection is visible.
|
||||
const http = require('http');
|
||||
const { WebSocketServer } = require('ws');
|
||||
|
||||
const server = http.createServer((req, res) => {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('ws-echo: connect with a WebSocket to this address\n');
|
||||
});
|
||||
const wss = new WebSocketServer({ server });
|
||||
wss.on('connection', (ws) => {
|
||||
ws.send('hello');
|
||||
ws.on('message', (m) => ws.send('echo:' + m));
|
||||
const t = setInterval(() => ws.send('tick:' + Date.now()), 5000);
|
||||
ws.on('close', () => clearInterval(t));
|
||||
});
|
||||
server.listen(process.env.PORT || 8080);
|
||||
Reference in New Issue
Block a user