-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathserver.js
185 lines (169 loc) · 4.85 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var express = require('express');
var app = express();
var http = require('http');
var fs = require('fs');
var IO = require('socket.io');
var { API_PORT } = require('./configure');
/*var options = {
key: fs.readFileSync('./ssl/key.pem'),
cert: fs.readFileSync('./ssl/cert.pem'),
passphrase: '123456789'
};*/
app.use(express.static('dist'));
var server = http.createServer(app).listen(API_PORT);
console.log('The HTTPS server is up and running');
var io = IO(server);
console.log('Socket Secure server is up and running.');
// All joined users
var allUsers = {};
// All joined sockets
var allSockets = {};
io.on('connect', function(socket) {
var user = ''; // current joined user
socket.on('message', function(data) {
var data = JSON.parse(data);
switch (data.event) {
// When has new user join in
case 'join':
user = data.name;
// Duplicated user name is not allowed
if (allUsers[user]) {
sendTo(socket, {
event: 'join',
message: '该用户名已存在, 请重新输入',
success: false,
});
} else {
console.log('User joined', data.name);
// Save users info
allUsers[user] = true; // 'true' means has not call, 'false' means calling
allSockets[user] = socket;
socket.name = user;
showUserInfo(allUsers);
sendTo(socket, {
event: 'join',
allUsers: allUsers,
success: true,
});
}
break;
case 'call':
var conn = allSockets[data.connectedUser];
sendTo(conn, {
event: 'call',
name: socket.name,
});
break;
case 'offer':
// i.e. UserA wants to call UserB
console.log('Sending offer to: ', data.connectedUser);
//if UserB exists then send him offer details
var conn = allSockets[data.connectedUser];
allUsers[user] = false;
if (conn != null) {
showUserInfo(allUsers);
// Setting that UserA connected with UserB
socket.otherName = data.connectedUser;
sendTo(conn, {
event: 'offer',
offer: data.offer,
name: socket.name,
});
} else {
sendTo(socket, {
event: 'msg',
message: 'Not found this name',
});
}
break;
case 'accept':
var conn = allSockets[data.connectedUser];
if (conn != null) {
if (data.accept) {
sendTo(conn, {
event: 'accept',
accept: true,
});
} else {
allUsers[data.connectedUser] = true;
sendTo(conn, {
event: 'accept',
accept: false,
});
}
}
break;
case 'answer':
console.log('Sending answer to: ', data.connectedUser);
// i.e. UserB answers UserA
var conn = allSockets[data.connectedUser];
allUsers[user] = false;
if (conn != null) {
showUserInfo(allUsers);
socket.otherName = data.connectedUser;
sendTo(conn, {
event: 'answer',
answer: data.answer,
});
}
break;
case 'candidate':
console.log('Sending candidate to:', data.connectedUser);
var conn1 = allSockets[data.connectedUser];
var conn2 = allSockets[socket.otherName];
if (conn1 != null) {
sendTo(conn1, {
event: 'candidate',
candidate: data.candidate,
});
} else {
sendTo(conn2, {
event: 'candidate',
candidate: data.candidate,
});
}
break;
case 'leave':
console.log('Disconnecting from', data.connectedUser);
var conn = allSockets[data.connectedUser];
allUsers[socket.name] = true;
allUsers[data.connectedUser] = true;
socket.otherName = null;
// Notify the other user so he can disconnect his peer connection
if (conn != null) {
showUserInfo(allUsers);
sendTo(conn, {
event: 'leave',
});
}
break;
}
});
socket.on('disconnect', function() {
if (socket.name) {
delete allUsers[socket.name];
delete allSockets[socket.name];
showUserInfo(allUsers);
if (socket.otherName) {
console.log('Disconnecting from ', socket.otherName);
var conn = allSockets[socket.otherName];
allUsers[socket.otherName] = true;
socket.otherName = null;
if (conn != null) {
sendTo(conn, {
type: 'leave',
});
}
}
}
});
});
function showUserInfo(allUsers) {
sendTo(io, {
event: 'show',
allUsers: allUsers,
});
}
function sendTo(connection, message) {
connection.send(message);
}