-
Notifications
You must be signed in to change notification settings - Fork 0
/
signaling.js
146 lines (116 loc) · 3.96 KB
/
signaling.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
var https = require('https')
var fs = require('fs')
var WebSocketServer = require('ws').Server;
let app = https
.createServer(
{
key: fs.readFileSync(
'/etc/letsencrypt/live/rarywebrtc.xyz/privkey.pem'),
cert: fs.readFileSync(
'/etc/letsencrypt/live/rarywebrtc.xyz/fullchain.pem')
// key: fs.readFileSync("/etc/nginx/cert.key"),
// cert: fs.readFileSync("/etc/nginx/cert.crt")
},
(req, res) => {
// ダミーリクエスト処理
res.writeHead(200);
res.end('All glory to WebSockets!\n');
})
.listen(8080);
// creating a websocket server at port 9090
var wss = new WebSocketServer({server: app});
// all connected to the server users
var users = {};
// when a user connects to our sever
wss.on('connection', function(connection) {
console.log('User connected');
// when server gets a message from a connected user
connection.on('message', function(message) {
var data;
// accepting only JSON messages
try {
data = JSON.parse(message);
} catch (e) {
console.log('Invalid JSON');
data = {};
}
// switching type of the user message
switch (data.type) {
// when a user tries to login
case 'login':
console.log('User logged', data.name);
// if anyone is logged in with this username then refuse
if (users[data.name]) {
sendTo(connection, {type: 'login', success: false});
} else {
// save user connection on the server
users[data.name] = connection;
connection.name = data.name;
sendTo(connection, {type: 'login', success: true});
}
break;
case 'offer':
// for ex. UserA wants to call UserB
console.log('Sending offer to: ', data.name);
// if UserB exists then send him offer details
var conn = users[data.name];
if (conn != null) {
// setting that UserA connected with UserB
connection.otherName = data.name;
sendTo(
conn, {type: 'offer', offer: data.offer, name: connection.name});
}
break;
case 'answer':
console.log('Sending answer to: ', data.name);
// for ex. UserB answers UserA
var conn = users[data.name];
if (conn != null) {
connection.otherName = data.name;
sendTo(conn, {type: 'answer', answer: data.answer});
}
break;
case 'candidate':
console.log('Sending candidate to:', data.name);
var conn = users[data.name];
if (conn != null) {
sendTo(conn, {type: 'candidate', candidate: data.candidate});
}
break;
case 'leave':
console.log('Disconnecting from', data.name);
if (data.name) {
var conn = users[data.name];
conn.otherName = null;
// notify the other user so he can disconnect his peer connection
if (conn != null) {
sendTo(conn, {type: 'leave'});
}
}
break;
default:
sendTo(
connection,
{type: 'error', message: 'Command not found: ' + data.type});
break;
}
});
// when user exits, for example closes a browser window
// this may help if we are still in "offer","answer" or "candidate" state
connection.on('close', function() {
if (connection.name) {
delete users[connection.name];
if (connection.otherName) {
console.log('Disconnecting from ', connection.otherName);
var conn = users[connection.otherName];
if (conn != null) {
conn.otherName = null;
sendTo(conn, {type: 'leave'});
}
}
}
});
});
function sendTo(connection, message) {
connection.send(JSON.stringify(message));
}