-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
94 lines (77 loc) · 2.08 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
var express = require("express"),
http = require("http");
var app = express();
var server = http.createServer(app);
//var server = app;
var io = require("socket.io")(server);
var bodyParser = require("body-parser");
server.listen(process.env.PORT || 81);
//io.set("log level", 1);
app.get("/", function (req, res) {
res.redirect("game.html");
});
//app.use(express.methodOverride());
app.use(bodyParser.json());
//app.use('/lib', express.static(__dirname + '/lib'));
app.use("/", express.static(__dirname + "/"));
// app.use(
// express.errorHandler({
// dumpExceptions: true,
// showStack: true,
// })
// );
//app.use(app.router);
//});
var ids = {};
function getId() {
var id;
do {
id = _generateId();
} while (ids[id]);
ids[id] = true;
return id;
}
function _generateId() {
return (
"0000" + ((Math.random() * Math.pow(36, 4)) << 0).toString(36)
).substr(-4);
}
io.sockets.on("connection", function (socket) {
socket.pid = getId();
socket.on("join", function (data) {
socket.room = data.room || 1;
socket.join(socket.room);
console.log("Player " + socket.pid + " joined the room " + data.room + ".");
socket.emit("pid", {
pid: socket.pid,
});
//add other players in the room
var players = io.sockets.adapter.rooms.get(socket.room);
for (let id of players) {
let player = io.sockets.sockets.get(id);
console.log(player);
if (player.pid != socket.pid)
socket.emit("player-join", {
pid: player.pid,
});
}
//notify others about the new player
socket.broadcast.to(socket.room).emit("player-join", {
pid: socket.pid,
});
});
socket.on("data", function (data) {
socket.broadcast.to(socket.room).volatile.emit("data", data);
//if (data.fr)
// console.log(data);
});
socket.on("disconnect", function () {
console.log("Player " + socket.pid + " disconnected.");
socket.broadcast.to(socket.room).emit("player-disconnected", {
pid: socket.pid,
});
});
socket.on("ping", function (data) {
socket.emit("ping", data);
});
});