clone3 commited on
Commit
cdb67ec
·
verified ·
1 Parent(s): df83c50

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +13 -22
server.js CHANGED
@@ -1,28 +1,19 @@
1
- const express = require('express');
2
- const http = require('http');
3
- const socketIo = require('socket.io');
4
 
5
- const app = express();
6
- const server = http.createServer(app);
7
- const io = socketIo(server);
8
 
9
- let visitorCount = 0;
 
10
 
11
- app.get('/', (req, res) => {
12
- res.json({ message: 'WebSocket server is running', visitorCount });
13
- });
14
-
15
- io.on('connection', (socket) => {
16
- visitorCount++;
17
- io.emit('visitorCount', visitorCount);
18
 
19
- socket.on('disconnect', () => {
20
- visitorCount--;
21
- io.emit('visitorCount', visitorCount);
22
- });
23
  });
24
 
25
- const PORT = process.env.PORT || 7860; // Default to 7860 for Hugging Face
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}`);