-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js.old
161 lines (116 loc) · 4.29 KB
/
server.js.old
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
/*
* Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree.
*/
/* eslint-env node */
'use strict';
//code initiaux
const app = require('express')();
const https = require('https');
const pem = require('pem');
const path = require('path');
const express = require('express');
let io;
pem.createCertificate({ days: 1, selfSigned: true }, function(err, keys) {
var options = {
key: keys.serviceKey,
cert: keys.certificate
};
// Create an HTTPS service.
//https.createServer(options, app).listen(8080);
const httpsServer = https.createServer(options, app);
io = require('socket.io')(httpsServer);
httpsServer.listen(5000);
handleRoutes();
handleSocketConnection();
// Les fonctions
function handleRoutes() {
app.use(express.static(path.join(__dirname, "/public")));
app.get('/bootstrap.css', function(req, res) {
//arrivé sur le repetoire racine du serveur, retourne le fichier index.html
res.sendFile(__dirname + '/public/css/bootstrap/bootstrap.css')
})
app.get('/styles.css', function(req, res) {
//arrivé sur le repetoire racine du serveur, retourne le fichier index.html
res.sendFile(__dirname + '/public/css/styles.css')
})
app.get('/main.js', function(req, res) {
//arrivé sur le repetoire racine du serveur, retourne le fichier index.html
res.sendFile(__dirname + '/public/scripts/main.js')
})
app.get('/adapter-latest.js', function(req, res) {
//arrivé sur le repetoire racine du serveur, retourne le fichier index.html
res.sendFile(__dirname + '/public/scripts/adapter-latest.js')
})
}
function handleSocketConnection() {
var activeSockets = [];
io.on("connection", socket => {
const existingSocket = activeSockets.find(
existingSocket => existingSocket === socket.id
);
if (!existingSocket) {
activeSockets.push(socket.id);
socket.emit("update-user-list", {
users: activeSockets.filter(
existingSocket => existingSocket !== socket.id
)
});
socket.broadcast.emit("update-user-list", {
users: [socket.id]
});
}
socket.on("call-user", data => {
socket.to(data.to).emit("call-made", {
offer: data.offer,
socket: socket.id
});
});
socket.on("make-answer", data => {
socket.to(data.to).emit("answer-made", {
socket: socket.id,
answer: data.answer
});
});
socket.on("disconnect", () => {
activeSockets = activeSockets.filter(
existingSocket => existingSocket !== socket.id
);
socket.broadcast.emit("remove-user", {
socketId: socket.id
});
});
});
}
console.log('serving on https://localhost:5000');
});
/*
require('webrtc-adapter')
const app = require('app')()
const http = require('http').createServer(app)
//routes
//renvoi l'application
app.get('/', function(req, res) {
//arrivé sur le repetoire racine du serveur, retourne le fichier index.html
res.sendFile(__dirname + '/index.html')
})
app.get('/css/bootstrap.css', function(req, res) {
//arrivé sur le repetoire racine du serveur, retourne le fichier index.html
res.sendFile(__dirname + '/css/bootstrap.css')
})
app.get('/js/main.js', function(req, res) {
//arrivé sur le repetoire racine du serveur, retourne le fichier index.html
res.sendFile(__dirname + '/js/main.js')
})
app.get('/js/adapter.js', function(req, res) {
//arrivé sur le repetoire racine du serveur, retourne le fichier index.html
res.sendFile(__dirname + '/js/adapter.js')
})
//lance le serveur web et écoute l'adresse
http.listen(3000, function() {
console.log('Serveur tournant sur 3000')
})
*/