-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
137 lines (108 loc) · 3.02 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
import express from 'express';
import { createServer } from 'node:http';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { Server } from 'socket.io';
import {writeFileSync} from "fs"
// config
const port = process.env.PORT || 3000;
// const ip = process.env.IP || 'localhost';
const ip = process.env.IP || '192.168.2.101';
const app = express();
app.use(express.static('public'));
const server = createServer(app);
const io = new Server(server);
// serve public dir
const __dirname = dirname(fileURLToPath(import.meta.url));
app.get('/', (req, res) => {
res.sendFile(join(__dirname, 'index.html'));
});
// returns id from headers
async function computeUserIdFromHeaders(headers) {
return headers.id
}
// main
io.on('connection', async (socket) => {
// connection
console.log(`Client (${socket.id }) is now connected`);
// send back id to user only
const userId = await computeUserIdFromHeaders(socket);
socket.join(userId);
io.to(userId).emit("welcome", userId);
// message
socket.on('message', (msg) => {
console.log("MEssage !");
console.log(msg);
io.emit('message', msg);
});
// video
socket.on('video', (msg) => {
console.log("Vidéo !");
console.log(msg);
io.emit('video', msg);
});
socket.on('special', (msg) => {
console.log("Special !");
console.log(msg);
io.of("/special").to('/special').emit('special', msg);
});
// disconnection
socket.on('disconnect', () => {
console.log('user disconnected');
});
// feeedback
socket.on('feeedback', () => {
console.log('feeedback');
});
});
io.of("/special").on("connection", (socket) => {
// print users
const userId = socket.id;
console.log(`Special user (${userId}) is now connected`);
socket.join("/special");
io.of("/special").to("/special").emit("welcome", userId);
// disconnection
socket.on('disconnect', () => {
console.log('Special user disconnected');
});
// printing signal
socket.on('Specializing', (msg) => {
console.log('Specializing !');
console.log(msg);
io.to(msg.userid).emit('Received specializing', { }, (err, responses) => {
console.log(err);
});
});
});
// server.listen(3000, () => {
server.listen(port, ip, function () {
console.log(`Server is running at http://${ip}:${port}`);
});
// receive an base64 image and write it to disk
function writeMessage(msg){
console.log("writeMessage");
// filename
const p = "export/"+ new Date().getTime() + ".png";
const pp = "public/" + p;
// parse b64
var d = msg.dataURL;
var data = d.split(',')[1];
var buf = Buffer.from(data, 'base64');
// write file
writeFileSync(pp, buf, err => {
console.log("Wrote Message");
if (err) {
console.error(err);
} else {
// file written successfully
console.log("file written successfully");
}
});
// send feedback
io.emit('feedback', {
url: `http://${ip}:${port}/${p}`,
from: msg.userid,
}, (err, responses) => {
console.log(err);
});
}