Update server.js
Browse files
server.js
CHANGED
@@ -1,50 +1,22 @@
|
|
1 |
-
const
|
2 |
-
const
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
const
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
}
|
24 |
-
|
25 |
-
onLeave(client, consented) {
|
26 |
-
console.log(client.sessionId, "left!");
|
27 |
-
}
|
28 |
-
|
29 |
-
onMessage(client, message) {
|
30 |
-
console.log(client.sessionId, "sent message", message);
|
31 |
-
this.broadcast("messages", message);
|
32 |
-
}
|
33 |
-
|
34 |
-
onDispose() {
|
35 |
-
console.log("Room disposed!");
|
36 |
-
}
|
37 |
-
}
|
38 |
-
|
39 |
-
// Register the room handler
|
40 |
-
gameServer.define("my_room", MyRoom);
|
41 |
-
|
42 |
-
// Serve static files (optional)
|
43 |
-
app.use(express.static("public"));
|
44 |
-
app.use("/colyseus", monitor());
|
45 |
-
|
46 |
-
// Start the server
|
47 |
-
const port = process.env.PORT || 7860;
|
48 |
-
server.listen(port, () => {
|
49 |
-
console.log(`Listening on ws://localhost:${port}`);
|
50 |
-
});
|
|
|
1 |
+
const express = require('express')
|
2 |
+
const webserver = express()
|
3 |
+
.use((req, res) =>
|
4 |
+
res.sendFile('/websocket-client.html', { root: __dirname })
|
5 |
+
)
|
6 |
+
.listen(7860, () => console.log(`Listening on ${7860}`))
|
7 |
+
const { WebSocketServer } = require('ws')
|
8 |
+
const sockserver = new WebSocketServer({ port: 443 })
|
9 |
+
sockserver.on('connection', ws => {
|
10 |
+
console.log('New client connected!')
|
11 |
+
ws.send('connection established')
|
12 |
+
ws.on('close', () => console.log('Client has disconnected!'))
|
13 |
+
ws.on('message', data => {
|
14 |
+
sockserver.clients.forEach(client => {
|
15 |
+
console.log(`distributing message: ${data}`)
|
16 |
+
client.send(`${data}`)
|
17 |
+
})
|
18 |
+
})
|
19 |
+
ws.onerror = function () {
|
20 |
+
console.log('websocket error')
|
21 |
+
}
|
22 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|