forked from barakpahima/kurento-webRTC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
123 lines (101 loc) · 3.34 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
const express = require('express'),
session = require('express-session'),
ws = require('ws'),
KurentoClient = require('./Kurento/KurentoClient');
const KURENTO_WS_URL = 'ws://localhost:8888/kurento'; // Kurento url, if the kurento is running in other machine then replace localhost with the machine's IP.
// Express
let app = express(),
sessionHandler = new session({
secret: 'none',
rolling: true,
resave: true,
saveUninitialized: true
});
app.use(sessionHandler);
// Static
app.use(express.static('client'));
app.listen(3000, function () {
console.log('listenning at 3000');
});
//
// Web Socket
//
let wss = new ws.Server({
port: 8080,
});
wss.on('connection', (newSocket, req) => {
var sessionId,
request = req,
response = {
writeHead: {}
};
sessionHandler(request, response, function (err) {
sessionId = request.sessionID;
console.log('Connection received with sessionId ' + sessionId);
});
// sessionHandler(request, response).catch(err => {
// console.log(err);
// });
sessionId = request.sessionID;
console.log('Connection received with sessionId ' + sessionId);
// create a new KurentoClient for each client
let kClient = new KurentoClient(KURENTO_WS_URL, newSocket);
newSocket.on('open', () => {
console.log(`connected to ${req.connection.remoteAddress}`);
});
newSocket.on('close', () => {
console.log('disconnected');
kClient.destroyPipeline(sessionId);
});
newSocket.on('error', (err) => {
console.log(err);
kClient.destroyPipeline(sessionId);
});
//
// Message
//
newSocket.on('message', function onMessage(msg) {
let parsedMsg = JSON.parse(msg);
switch (parsedMsg.id) {
case 'start':
console.log('received "start" message ');
kClient.createPipeline(sessionId, parsedMsg.sdpOffer, function (err, sdpAnswer) {
let response;
if (err) {
console.error(err);
response = JSON.stringify({
id: 'error',
message: err
});
}
else {
if (parsedMsg.sdpOffer != null) {
response = JSON.stringify({
id: 'sdpAnswer',
sdpAnswer: sdpAnswer
});
}
else {
console.log('f');
response = JSON.stringify({});
}
}
return newSocket.send(response);
});
break;
case 'stop':
kClient.destroyPipeline(sessionId);
break;
case 'iceCandidate':
console.log('received ice candidate');
kClient.addClientIceCandidate(sessionId, parsedMsg.candidate);
break;
default:
newSocket.send(JSON.stringify({
id: 'error',
message: 'Invalid message '
}));
break;
}
});
});