import { exposeWebSocket } from 'nanoexpress/exposes/index.js';
app.ws('/ws', (req, res) => exposeWebSocket({
console.log('Connecting...');
res.on('connection', (ws) => {
console.log('Connected');
ws.on('message', (msg) => {
console.log('Message received', msg);
ws.send(msg);
});
ws.on('close', (code, message) => {
console.log('Connection closed', { code, message });
});
});
res.on('upgrade', () => {
console.log('Connection upgrade');
});
}));
app.ws('/', {
/* Options */
compression: uWS.SHARED_COMPRESSOR,
maxPayloadLength: 16 * 1024 * 1024,
idleTimeout: 10,
/* Handlers */
open: (ws) => {
console.log('A WebSocket connected!');
},
message: (ws, message, isBinary) => {
/* Ok is false if backpressure was built up, wait for drain */
let ok = ws.send(message, isBinary);
},
drain: (ws) => {
console.log('WebSocket backpressure: ' + ws.getBufferedAmount());
},
close: (ws, code, message) => {
console.log('WebSocket closed');
}
});