Kano001 commited on
Commit
d8d1171
·
verified ·
1 Parent(s): 2059092

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +43 -14
server.js CHANGED
@@ -1,19 +1,48 @@
1
- const WebSocket = require('ws');
 
 
2
 
3
- const wss = new WebSocket.Server({ port: 7860 });
 
4
 
5
- wss.on('connection', function connection(ws) {
6
- console.log('A new client connected!');
 
 
 
7
 
8
- ws.on('message', function incoming(message) {
9
- console.log('Received:', message);
10
- // Echo the message back to the client
11
- ws.send(`Server received: ${message}`);
12
- });
13
 
14
- ws.on('close', function () {
15
- console.log('Client has disconnected');
16
- });
17
- });
 
 
 
 
 
 
 
 
18
 
19
- console.log('WebSocket server is listening on ws://localhost:8080');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const colyseus = require("colyseus");
2
+ const http = require("http");
3
+ const express = require("express"); // optional
4
 
5
+ // Initialize the Express app (optional)
6
+ const app = express();
7
 
8
+ // Create HTTP & WebSocket servers
9
+ const server = http.createServer(app);
10
+ const gameServer = new colyseus.Server({
11
+ server: server,
12
+ });
13
 
14
+ // Define a room handler
15
+ class MyRoom extends colyseus.Room {
16
+ onCreate(options) {
17
+ console.log("Room created!", options);
18
+ }
19
 
20
+ onJoin(client, options) {
21
+ console.log(client.sessionId, "joined!");
22
+ }
23
+
24
+ onLeave(client, consented) {
25
+ console.log(client.sessionId, "left!");
26
+ }
27
+
28
+ onMessage(client, message) {
29
+ console.log(client.sessionId, "sent message", message);
30
+ this.broadcast("messages", message);
31
+ }
32
 
33
+ onDispose() {
34
+ console.log("Room disposed!");
35
+ }
36
+ }
37
+
38
+ // Register the room handler
39
+ gameServer.define("my_room", MyRoom);
40
+
41
+ // Serve static files (optional)
42
+ app.use(express.static("public"));
43
+
44
+ // Start the server
45
+ const port = process.env.PORT || 7860;
46
+ server.listen(port, () => {
47
+ console.log(`Listening on ws://localhost:${port}`);
48
+ });