blob: e672f8a57320b0468d131eed0ee78e10281b20a1 [file] [log] [blame]
Charliec2c012f2022-10-05 14:09:28 -04001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
18
19/* TODO remove this file.
20This is a temporary socker server only used for testing the webrtc
21connection between two local client. We are using this server while
22we develop the real communication server with the deamon */
23
24import cors from 'cors';
25import express from 'express';
26import http from 'http';
27import os from 'os';
28import { Server } from 'socket.io';
29
30let app = express();
31app.use(cors());
32const server = http.createServer(app);
33
34const io = new Server(server, {
35 cors: {
simonce2c0c42022-11-02 17:39:31 -040036 origin: '*',
Charliec2c012f2022-10-05 14:09:28 -040037 },
38});
39
40const PORT = process.env.PORT || 8080;
41
42const ip = Object.values(os.networkInterfaces())
43 .flat()
44 .find((i) => i.family === 'IPv4' && !i.internal).address;
45
46io.on('connection', (socket) => {
47 console.log('Connection to socket from ' + socket.id);
48
49 socket.on('offer', (sdp) => {
50 console.log('offer: ' + socket.id);
51 socket.broadcast.emit('getOffer', sdp);
52 });
53
54 socket.on('answer', (sdp) => {
55 console.log('answer: ' + socket.id);
56 socket.broadcast.emit('getAnswer', sdp);
57 });
58
59 socket.on('candidate', (candidate) => {
60 console.log('candidate: ' + socket.id);
61 socket.broadcast.emit('getCandidate', candidate);
62 });
63
64 socket.on('disconnect', () => {
65 console.log(`${socket.id} exit`);
66 });
67});
68
69server.listen(PORT, () => {
70 console.log(`server running on ${ip}:${PORT}`);
71});