-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
78 lines (70 loc) · 1.79 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
var app = require("express")();
var http = require("http").Server(app);
var io = require("socket.io")(http);
var RPG = {
users: {}
};
app.get("*", function(req, res) {
if(req.url.indexOf("/.") >= 0) {
res.send("nope.");
} else {
res.sendFile(__dirname + req.url);
}
});
io.on("connection", function(socket) {
//console.log("someone's here");
io.emit("users", RPG.users);
//io.emit("to everyone");
//socket.broadcast.emit("to everyone else");
//io.sockets.connected[id].emit("to one client", socket.id);
socket.on("signin", function(name) {
//console.log("sign in", socket.id);
//console.log(name, socket);
RPG.users[socket.id] = {
name: name || "Anonymous"
};
io.emit("users", RPG.users);
});
socket.on("invite", function(id) {
//console.log("invite", id);
if(io.sockets.connected[id]) {
io.sockets.connected[id].emit("invite", socket.id);
}
});
socket.on("accept", function(id) {
//console.log("accept", id);
if(RPG.users[id]) {
delete RPG.users[id];
}
if(RPG.users[socket.id]) {
delete RPG.users[socket.id];
}
if(io.sockets.connected[id]) {
io.sockets.connected[id].emit("accept", socket.id);
}
io.emit("users", RPG.users);
});
socket.on("reject", function(id) {
//console.log("reject", id);
if(io.sockets.connected[id]) {
io.sockets.connected[id].emit("reject", socket.id);
}
});
socket.on("pages", function(data) {
data = data || {};
//console.log("pages from", data.id);
if(data.id && io.sockets.connected[data.id]) {
io.sockets.connected[data.id].emit("pages", data.pages);
}
});
socket.on("disconnect", function() {
//console.log("someone left");
if(RPG.users[socket.id]) {
delete RPG.users[socket.id];
}
io.emit("users", RPG.users);
});
});
http.listen(1139, function() {
console.log("listening on *:1139");
});