-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
82 lines (70 loc) · 2.14 KB
/
index.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
const express = require("express");
const WebSocket = require("ws");
const http = require("http");
const uuid = require("uuid");
const app = express();
const server = http.createServer(app);
const io = require("socket.io")(server);
const channels = {};
const hosts = {};
app.use("/", express.static("."));
io.on("connection", (socket) => {
const channels = Object.keys(hosts);
channels.forEach(id => socket.emit("new_channel", {
id
}));
socket.on("create", (data, fn) => {
if (hosts.hasOwnProperty(socket.id)) {
fn("createFailed");
} else {
hosts[socket.id] = socket;
socket.room = socket.id;
socket.join(socket.id);
socket.broadcast.emit("new_channel", {
id: socket.id
});
fn("createSuccess");
}
});
socket.on("join", (data, fn) => {
if (typeof hosts[data.id] !== "undefined") {
socket.room = data.id;
socket.join(data.id);
// get current model from host and send it to the client
hosts[data.id].emit("getInit", undefined, (response) => {
socket.emit("init", response);
});
}
});
socket.on("leave", (data, fn) => {
socket.room = undefined;
socket.leave(data.id);
});
socket.on("remove", (data, fn) => {
if (hosts.hasOwnProperty(socket.id)) {
delete hosts[socket.id];
socket.room = undefined;
socket.broadcast.emit("remove_channel", {
id: socket.id
});
fn("removeSuccess");
} else {
fn("removeFailed")
}
});
socket.on("vf_event", (data, fn) => {
socket.broadcast.to(socket.room).emit("vf_event", data);
});
// remove closed connections
socket.on("disconnect", () => {
if (hosts.hasOwnProperty(socket.id)) {
delete hosts[socket.id];
socket.broadcast.emit("remove_channel", {
id: socket.id
});
}
});
});
server.listen(80, () => {
console.log("Listening on %d", server.address().port);
});