-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (44 loc) · 1.27 KB
/
index.js
File metadata and controls
56 lines (44 loc) · 1.27 KB
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
const express = require("express")
const app = express()
var http = require('http').createServer(app);
var io = require('socket.io')(http);
app.use(express.static("./public"))
let model = [];
let X = "X";
let O = "O";
let BLANK = " ";
let BOARD_FULL = "BOARD FULL";
for (let i = 0; i < 9; i++) {
model[i] = BLANK;
}
app.get("/getModel", (req, res) => {
console.log("Someone got the model")
let toSend = JSON.stringify(model);
console.log(toSend);
res.send(toSend);
})
app.get("/setModel/:model", (req,res)=>{
model = req.params.model;
console.log(model);
res.send("ok");
})
io.on('connection', function(socket){
console.log('a user connected ' + socket.id);
socket.on('getModel', function(msg){
console.log("getModel")
socket.emit('model', JSON.stringify(model));
});
socket.on("setModel", function(msg){
console.log("set model", msg);
model = JSON.parse(msg);
socket.broadcast.emit("model", JSON.stringify(model));
})
socket.on('disconnect', function(){
console.log("disconnected");
})
});
http.listen(5555, function (err) {
// app.listen(5555, function (err) {
if (err) return console.error(err);
console.log("We are listening on http://localhost:5555");
})