-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
105 lines (85 loc) · 3.58 KB
/
server.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
95
96
97
98
99
100
101
102
103
104
105
require('dotenv/config')
const express = require('express')
const app = express()
const http = require('http').createServer(app)
const https = require('https')
const fs = require('fs')
const io = require('socket.io')(http)
const debug = process.env.DEBUG === 'true'
app.use(express.static('dist'))
// the robot creator view
app.get('/creator', function (req, res) {
res.sendFile(__dirname + '/creator/')
})
// the game view
app.get('/viewer', function (req, res) {
res.sendFile(__dirname + '/viewer/')
})
io.on('connection', (socket) => {
// NEW CONNECTION MAY BE FOR A ROBOT THAT ALREADY EXISTS
console.log('new connection')
// when the client tank gets a new socket id, we need to update it in the game too
socket.on('robot reconnected', (json) => {
let debug = JSON.parse(json)
console.log('robot socket updated ' + debug.socketid)
// tell the game that there is a new robot - todo FOR VIEWER
io.emit('robot reconnected', json, { for: 'everyone' });
});
// viewer window was refreshed - destroy all the old robots that were already running
socket.on('viewer refreshed', () => {
io.emit('viewer refreshed')
});
// todo game moet ook de socketid updaten, want die kan veranderd zijn
socket.on('robot created', (json) => {
let debug = JSON.parse(json)
console.log('robot created ' + debug.nickname)
// tell the game that there is a new robot - todo FOR VIEWER
io.emit('robot created', json, { for: 'everyone' });
});
socket.on('robot destroyed', (socketid) => {
// send message to this specific tank
io.to(socketid).emit("tank destroyed")
// io.sockets.socket(socketid).emit("tank destroyed")
// https://socket.io/docs/emit-cheatsheet/
});
// todo hier ook de socketid updaten, want die kan veranderd zijn
socket.on('robot updated', (json) => {
let debug = JSON.parse(json)
console.log('new program for ' + debug.nickname)
io.emit('robot updated', json, { for: 'everyone' })
});
socket.on('disconnect', () => {
console.log('user disconnected')
// TODO if the GAME VIEW has disconnected, send message to all robots
//
});
});
if(debug) {
console.log('debug mode');
const server = http.listen(3000, () => {
// console.log('viewer http://localhost:3000/viewer')
// console.log('creator http://localhost:3000/creator')
})
server.on('listening', () => {
const host = server.address().address === '::' ? 'localhost' : server.address().address; // Address could be '::' in case of IPv6, defaulting to localhost in such cases
console.log(`viewer http://${host}:${server.address().port}/viewer`)
console.log(`creator http://${host}:${server.address().port}/creator`)
})
} else {
const key = fs.readFileSync(process.env.KEY); // private key
const cert = fs.readFileSync(process.env.CERT ); // primary
const port = process.env.PORT || 8080
//const ca = fs.readFileSync('/encryption/DigiCertCA.crt' ); // intermediate
const options = {
key: key,
cert: cert,
// ca: ca
};
server = https.createServer(options, app).listen(port, () => {
console.log('server running at ' + port);
})
server.on('listening', () => {
console.log(`viewer https://${process.env.HOST}:${server.address().port}/viewer`)
console.log(`creator https://${process.env.HOST}:${server.address().port}/creator`)
})
}