-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtc_server.js
133 lines (118 loc) · 3.67 KB
/
rtc_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var sockio = require("socket.io");
var io = sockio.listen(8889);
console.log("Here!");
// Users
var users = {};
// Perhaps could store messages into mongodb
// Perhaps mongodb should be used from beginning
// Register user
function register(socket, msg, cont) {
if (!msg.id) {
cont({error:"Need to give identifier to register"});
return;
}
if (users[msg.id]) {
cont({error:"Already registered", nick: users[msg.id].nick});
return;
}
var user = {};
user.nick = msg.nick || "WTF?";
user.id = msg.id;
user.sockets = [];
user.queued_messages = [];
user.friends = [];
users[msg.id] = user;
cont({msg:"success", nick:user.nick});
// cont("success");
}
// Connect socket to user
function connect(socket, msg, cont) {
if (!msg.id || !users[msg.id]) {
cont({error:"Not registered"});
return;
}
if (socket.user_id) {
cont({error:"Already connected"});
return;
}
var user = users[msg.id];
user.sockets.push(socket);
socket.user_id = msg.id;
cont({msg:"success"});
}
// Call request, already has peerconnection open. The message should include a control message.
function call(socket, msg, cont) {
// Send to all sockets of user
if (!socket.user_id) {
cont({error:"Unconnected socket"});
return;
}
if (!msg.id || !users[msg.id]) {
cont({error:"Not registered"});
return;
}
var calling_user = users[socket.user_id];
msg.caller = {id:calling_user.id, nick: calling_user.nick};
// msg.caller_nick = calling_user.nick;
msg.caller_socket = socket.uniq_id;
var user = users[msg.id];
user.sockets.forEach(function (s) {
// To accept the call, just peer connection needs to be established, no more server interaction is needed
// Can the continuation be used several times?
if (s.uniq_id != socket.uniq_id) s.emit("call", msg, function (reply) {
reply.socket = s.uniq_id;
reply.user = {id: user.id, nick: user.nick};
cont(reply);
});
});
}
// Send control message
function control(socket, msg, cont) {
if (!msg.to || !sockets[msg.to]) {
if (cont) cont({error:"Unknown destination"});
return;
}
msg.from = socket.uniq_id;
sockets[msg.to].emit("control_call", msg);
}
function hangup(socket, msg, cont) {
if (!msg.to || !sockets[msg.to]) {
console.log(cont);
if (cont) cont({error:"Unknown destination"});
return;
}
msg.from = socket.uniq_id;
sockets[msg.to].emit("hangup_call", msg);
}
// Request users
function list(socket, msg, cont) {
var res = [];
try {
var regex = new RegExp(msg.search_string);
for (var i in users) {
var u = users[i];
if (u.nick.match(regex)) res.push({id:u.id, nick:u.nick});
}
}
// Bad regex
catch (e) {}
cont(res);
}
var sockets = ["foo"];
io.sockets.on("connection", function (socket) {
// socket.on("register", function (msg, cont) { cont("foo"); });
// New socket connection, add to list
socket.uniq_id = sockets.length;
sockets.push(socket);
// Register handlers
socket.on("register", function (msg, cont) {
register(socket, msg, cont);
});
socket.on("connect_user", function (msg, cont) {
connect(socket, msg, cont);
});
socket.on("list", function (msg, cont) { list(socket, msg, cont); });
socket.on("call", function (msg, cont) { call(socket, msg, cont); });
socket.on("control_call", function (msg, cont) { control(socket, msg, cont); });
socket.on("hangup_call", function (msg, cont) { hangup(socket, msg, cont); });
});