Update server.js
Browse files
server.js
CHANGED
|
@@ -1,28 +1,19 @@
|
|
| 1 |
-
const
|
| 2 |
-
const
|
| 3 |
-
const socketIo = require('socket.io');
|
| 4 |
|
| 5 |
-
const
|
| 6 |
-
const server = http.createServer(app);
|
| 7 |
-
const io = socketIo(server);
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
});
|
| 14 |
-
|
| 15 |
-
io.on('connection', (socket) => {
|
| 16 |
-
visitorCount++;
|
| 17 |
-
io.emit('visitorCount', visitorCount);
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
});
|
| 23 |
});
|
| 24 |
|
| 25 |
-
|
| 26 |
-
server.listen(PORT, () => {
|
| 27 |
-
console.log(`Server running on http://localhost:${PORT}`);
|
| 28 |
-
});
|
|
|
|
| 1 |
+
const WebSocket = require('ws');
|
| 2 |
+
const PORT = 7860;
|
|
|
|
| 3 |
|
| 4 |
+
const wss = new WebSocket.Server({ port: PORT });
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
wss.on('connection', (ws) => {
|
| 7 |
+
console.log('Client connected');
|
| 8 |
|
| 9 |
+
ws.on('message', (message) => {
|
| 10 |
+
console.log(`Received: ${message}`);
|
| 11 |
+
ws.send(`Echo: ${message}`);
|
| 12 |
+
});
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
ws.on('close', () => {
|
| 15 |
+
console.log('Client disconnected');
|
| 16 |
+
});
|
|
|
|
| 17 |
});
|
| 18 |
|
| 19 |
+
console.log(`WebSocket server is running on ws://localhost:${PORT}`);
|
|
|
|
|
|
|
|
|