-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.js
161 lines (123 loc) · 3.91 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
//Required Modules
require('dotenv').config()
const https = require('https')
const ws = require('ws')
const express = require('express')
const fs = require('fs')
// Import SSL Certificates
let cert_key = fs.readFileSync(process.env.CERT_KEY)
let cert_file = fs.readFileSync(process.env.CERT_FILE)
let page = fs.readFileSync('./index.htm')
let options = {key: cert_key, cert: cert_file}
const app = express()
app.use(express.static(__dirname + '/public'))
app.get('/', (req, res) => {
res.send(page.toString())
})
let httpsServer = https.createServer(options, app)
httpsServer.listen(process.env.PORT)
console.log(`The app is running on port ${httpsServer.address().port}`)
//WebSocket Connection
let WebSocketServer = ws.Server
let wsServer = new WebSocketServer({server: httpsServer, ssl: true})
let users = {}
wsServer.on('connection', (connection) => {
let data;
console.log('WebSocket Connected successfully');
connection.on('message', (message) => {
try {
data = JSON.parse(message);
}
catch (e) {
console.log('Invalid JSON data!');
data = {};
}
switch(data.type) {
case 'login':
console.log('Users logged: '+ data.name);
if (users[data.name]) {
sendTo(connection, {
type: 'login',
success: false
})
}
else {
users[data.name] = connection;
connection.name = data.name;
sendTo(connection, {
type: 'login',
success: true
})
}
break;
case 'offer':
console.log('Sending calling offer to '+ data.name)
var conn = users[data.name]
if (conn != null) {
connection.otherName = data.name;
sendTo(conn, {
type: 'offer',
offer: data.offer,
name: connection.name
})
}
break;
case 'answer':
console.log('Sending answer to '+ data.name)
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('Disconecting from '+ data.name);
var conn = users[data.name];
conn.otherName = null;
if (conn != null) {
sendTo(conn, {
type: 'leave;'
})
}
break;
default:
sendTo(connection, {
type: 'error',
message: 'Error, command not found: '+ data.type
})
}
})
connection.on('close', () => {
if (connection.name) {
delete users[connection.name];
if (connection.otherName) {
console.log('Disconnecting from '+ connection.otherName);
var conn = users[connection.otherName];
conn.otherName = null;
sendTo(conn, {
type: 'leave'
})
}
}
})
connection.send(JSON.stringify({
source: 'Server',
msg: 'Hello Vid streamer!'
}))
})
function sendTo(connection, message) {
connection.send(JSON.stringify(message));
}