-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
204 lines (149 loc) · 4.53 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const http = require('http');
const fs = require('fs');
const path = require('path');
const WebSocketServer = require('websocket').server;
const indexPath = path.join(__dirname,'/public/index.html');
const jsPath = path.join(__dirname,'../dist/bundle.js');
const cssPath = path.join(__dirname,'../dist/style.css');
let carData = [];
let clients = []
const server = http.createServer((req,res) => {
if(req.url==="/"){
fs.readFile(indexPath, (err, file) => {
if(err) {
res.send(500,{error: err});
}
res.writeHeader(200, {"Content-Type": "text/html"});
res.write(file);
res.end()
})
}
if(req.url==="/bundle.js") {
fs.readFile(jsPath, (err, file) => {
if(err) {
console.log("Error getting the javaScript")
}
res.writeHeader(200, {"Content-Type": "text/javascript"});
res.write(file);
res.end();
})
}
if(req.url==="/styles.css") {
fs.readFile(cssPath, (err, file ) => {
if(err) {
console.log("Error getting the css")
}
res.writeHeader(200, {"Content-Type": "text/css"});
res.write(file);
res.end();
})
}
});
server.listen(3000,'127.0.0.1');
const broadcastExcept = (action,data,connection) => {
clients.map( socket => {
if(!isEquivalent(connection,socket))
socket.send(JSON.stringify(
{
action,
data
}));
})
}
const broadcastAll = (action,data) => {
clients.map( socket => {
socket.send(JSON.stringify(
{
action,
data
}));
})
}
const updateCars = (newData) => {
return carData.map(car => {
if(car.id === newData.id){
return newData;
}
return car;
})
}
const webSocketServer = new WebSocketServer({httpServer:server});
webSocketServer.on('request', request => {
const data = {
id: uuid(),
xPos: randomXPos(),
yPos: randomXPos(),
angle: randomAngle()
}
broadcastAll("NEW_PLAYER",data);
var connection = request.accept('echo-protocol', request.origin);
console.log((new Date()) + ' Connection accepted.');
carData.push(data);
console.log('8888888888888888888888888888888',carData);
connection.send(JSON.stringify(
{
action: 'INIATE',
data,
otherPlayerData:carData
}))
// setInterval(() => {
// connection.send(JSON.stringify({
// action:"test",
// data:data.id
// }))
// }, 1000);
clients.push(connection);
connection.on('message', message => {
const _message = JSON.parse(message.utf8Data);
switch(_message.action){
case "UPDATE_PLAYER":
carData = updateCars(_message.data)
console.log("New Canvas Positions", carData);
broadcastExcept("UPDATE_CANVAS",_message.data,connection);
break;
default:
// console.log(_message);
}
});
connection.on('close', (reasonCode, description) => {
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
});
const uuid = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
const randomXPos = () => {
return Math.floor((Math.random() * 200) + 25);
}
const randomYPos = () => {
return Math.floor((Math.random() * 200) + 25);
}
const randomAngle = () => {
return Math.floor((Math.random() * 360));
}
const randomColor = () => {
}
function isEquivalent(a, b) {
// Create arrays of property names
var aProps = Object.getOwnPropertyNames(a);
var bProps = Object.getOwnPropertyNames(b);
// If number of properties is different,
// objects are not equivalent
if (aProps.length != bProps.length) {
return false;
}
for (var i = 0; i < aProps.length; i++) {
var propName = aProps[i];
// If values of same property are not equal,
// objects are not equivalent
if (a[propName] !== b[propName]) {
return false;
}
}
// If we made it this far, objects
// are considered equivalent
return true;
}