-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
35 lines (26 loc) · 807 Bytes
/
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
/*const express=require('express');
const app=express();
app.use(function (req,res,next){
res.header("Access-Control-Alow-Origin","*");
res.header("Access-Control-Allow-Headers","Origin,X-Requated-With,Content-Type,Accept");
next();
});
*/
const io = require('socket.io')();
const{createGameState,gameLoop} = require ('./game');
const {FRAME_RATE} = require ('./constants');
io.on('connection',client=> {
const state = createGameState();
startGameInterval(client,state);
});
function startGameInterval(client,state) {
const intervalId = setInterval(() =>{
const winner = gameLoop(state);
if (!winner){
io.emit('gameState',JSON.stringify(state));
}else {client.emit('gameOver');
clearInterval(intervalId);
}
},1000/FRAME_RATE);
}
io.listen(3000);