-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignalling.js
127 lines (106 loc) · 4.79 KB
/
signalling.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
"use strict";
(function(){
function itself(server, sessionParser, UserEventHandlers){
const HTTPS_PORT = 443;
const fs = require('fs');
const EventEmitter = require('events');
const https = require('https');
const WebSocket = require('ws');
const WebSocketServer = WebSocket.Server;
// Yes, SSL is required
const serverConfig = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt'),
};
// ----------------------------------------------------------------------------------------
// Create a server for handling websocket calls
var wss = new WebSocketServer({server: server});
// WebSocket Message Handler (per user)
class UserEventHandler extends EventEmitter {
constructor(ws, username) {
super();
this.partner;
this.help_request = true;
this.displayed_description = ' - ' + username;
this.username = username;
this.info_packet;
ws.on('message', (message)=>{
console.log(message);
var m = JSON.parse(message); // message = {event: event, message: [args...]}
this.emit.apply(this, [m.event].concat(m.message)); //This should emulate this.emit(event, arg1, arg2...)
});
// Self triggered events
this.on('ownSessionDescription', (description)=>{
if(this.partner) {
this.partner.emit('otherSessionDescription', description);
} else {
console.error("[ERROR!] " + ws.upgradeReq.session.username + " has no partner.");
}
});
this.on('infoPacket', (info_packet)=>{
this.info_packet = info_packet;
this.displayed_description = info_packet.category + ' - ' + this.username;
});
this.on('requestPeerConnection', (peer)=>{
this.help_request = false;
this.partner = UserEventHandlers[peer];
this.partner.partner = this;
this.partner.emit('peerConnection');
ws.send(JSON.stringify({
event: 'infoPacket',
message: [this.partner.info_packet]
}));
});
this.on('ownICECandidate', (candidate)=>{
if(this.partner) {
this.partner.emit('otherICECandidate', candidate);
} else {
console.error("[ERROR!] " + ws.upgradeReq.session.username + " has no partner.");
}
});
// Partner triggered events
this.on('otherICECandidate', (candidate)=>{
ws.send(JSON.stringify({
event: 'ICECandidate',
message: [candidate]
}));
});
this.on('otherSessionDescription', (description)=>{
ws.send(JSON.stringify({
event: 'sessionDescription',
message: [description]
}));
});
this.on('peerConnection', ()=>{
this.help_request = false; //Peer has connected, no longer needs help
ws.send(JSON.stringify({
event: 'peerConnection'
//message not specified to test client-side robustness (+ no message is needed here)
}));
});
}
}
// ----------------------------------------------------------------------------------------
wss.on('connection', function(ws) {
var username;
sessionParser(ws.upgradeReq, {}, function(){
console.log("New websocket connection");
username = ws.upgradeReq.session.username;
UserEventHandlers[ws.upgradeReq.session.username] = new UserEventHandler(ws, username);
});
ws.on('close', function(){
console.log("CLOSING");
delete UserEventHandlers[username];
});
});
wss.broadcast = function(data) {
this.clients.forEach(function(client) {
if(client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
};
console.log('Server running. Visit https://localhost:' + HTTPS_PORT + ' in Firefox/Chrome (note the HTTPS; there is no HTTP -> HTTPS redirect!)');
}
module.exports = itself;
})();