-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgameserver.js
94 lines (73 loc) · 2.05 KB
/
gameserver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// gameserver.js
import AgonesSDK from '@google-cloud/agones-sdk'
import express from 'express';
import { createServer } from 'http'
import { Server } from 'socket.io'
const port = process.env.PORT || 7654;
let agonesSDK = new AgonesSDK();
let status = null;
const users = {}
let messages = []
const app = express()
const server = createServer(app)
const io = new Server(server, {
cors: {
origin: ["http://localhost:*", "https://agones-socketio.netlify.app/:*"],
methods: ["GET", "POST"]
// allowedHeaders: ["Access-Control-Allow-Origin"],
}
})
const authenticate = (name) => {
if(!/^[a-zA-Z]\w*$/.test(name || "")){
return `Authentication attempt: Name ${name} is invalid`;
}
return null
}
io.on('connection', async (socket) => {
const socketId = socket.id;
const name = socket.handshake.auth.token
let err = authenticate(name)
if(err){
socket.emit("error", err)
socket.disconnect();
return;
}
users[socketId] = name;
console.log(`User ${name} connected`)
socket.emit('messages', messages)
if(status === "Ready"){
await agonesSDK.allocate();
}
socket.on('newMessage', (data) => {
console.log("new chat message: ", data)
messages.push(data);
io.emit('newMessage', data);
});
socket.conn.on("close", reason => {
console.log(`Socket closed. user: ${users[socket.id]}, reason: ${reason}`)
delete users[socketId]
if(Object.keys(users).length === 0){
console.log("All users have left the server")
console.log("Resetting server")
messages = []
agonesSDK.ready();
}
})
});
server.listen(port, () => {
console.log(`Server listening on port ${port}`)
});
const prepareGameserver = async () => {
console.log(`Connecting to the SDK server...`);
await agonesSDK.connect();
setInterval(() => {
agonesSDK.health();
}, 5000)
console.log("Getting ready...")
await agonesSDK.ready();
agonesSDK.watchGameServer(gameServer => {
status = gameServer.status.state
console.log("Gameserver Status", status)
})
};
prepareGameserver();