Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 1 | /* |
| 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. |
| 20 | This is a temporary socker server only used for testing the webrtc |
| 21 | connection between two local client. We are using this server while |
| 22 | we develop the real communication server with the deamon */ |
| 23 | |
| 24 | import cors from 'cors'; |
| 25 | import express from 'express'; |
| 26 | import http from 'http'; |
| 27 | import os from 'os'; |
| 28 | import { Server } from 'socket.io'; |
| 29 | |
| 30 | let app = express(); |
| 31 | app.use(cors()); |
| 32 | const server = http.createServer(app); |
| 33 | |
| 34 | const io = new Server(server, { |
| 35 | cors: { |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 36 | origin: '*', |
Charlie | c2c012f | 2022-10-05 14:09:28 -0400 | [diff] [blame] | 37 | }, |
| 38 | }); |
| 39 | |
| 40 | const PORT = process.env.PORT || 8080; |
| 41 | |
| 42 | const ip = Object.values(os.networkInterfaces()) |
| 43 | .flat() |
| 44 | .find((i) => i.family === 'IPv4' && !i.internal).address; |
| 45 | |
| 46 | io.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 | |
| 69 | server.listen(PORT, () => { |
| 70 | console.log(`server running on ${ip}:${PORT}`); |
| 71 | }); |