-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.js
157 lines (124 loc) · 5.06 KB
/
websocket.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
var socketio = require('socket.io')
const fs = require("fs");
const path = require("path");
module.exports.listen = function (app) {
io = socketio.listen(app);
// array of all lines drawn
var line_history = [];
// event-handler for new incoming connections
io.on('connection', function (socket) {
//------------------------------------------------------------//
console.log("connected")
socket.emit('refresh-msg', { wesh: 'whatever'});
socket.on('refresh-msg', function (data) {
console.log("wesh")
var readStream = fs.createReadStream(path.resolve(__dirname, './uploads/image.jpg'),{
encoding:'binary'
}), chunks = [];
var readStream2 = fs.createReadStream(path.resolve(__dirname, './uploads/image2.jpg'),{
encoding:'binary'
}), chunks2 = [];
var readStream3 = fs.createReadStream(path.resolve(__dirname, './uploads/image3.jpg'),{
encoding:'binary'
}), chunks3 = [];
var readStream4 = fs.createReadStream(path.resolve(__dirname, './uploads/image4.jpg'),{
encoding:'binary'
}), chunks4 = [];
// readStream.on('readable', function(){
// console.log('Image loading');
// })
var readStreamQR = fs.createReadStream(path.resolve(__dirname, './qrCodes/qrCode1.png'),{
encoding:'binary'
}), chunksQR = [];
var readStreamQR2 = fs.createReadStream(path.resolve(__dirname, './qrCodes/qrCode2.png'),{
encoding:'binary'
}), chunksQR2 = [];
var readStreamQR3 = fs.createReadStream(path.resolve(__dirname, './qrCodes/qrCode3.png'),{
encoding:'binary'
}), chunksQR3 = [];
var readStreamQR4 = fs.createReadStream(path.resolve(__dirname, './qrCodes/qrCode4.png'),{
encoding:'binary'
}), chunksQR4 = [];
readStream.on('data', function(chunk){
console.log('en cours')
chunks.push(chunk);
socket.emit('img-chunk',chunk);
})
readStream2.on('data', function(chunk){
console.log('en cours')
chunks2.push(chunk);
socket.emit('img-chunk2',chunk);
})
readStream3.on('data', function(chunk){
console.log('en cours')
chunks3.push(chunk);
socket.emit('img-chunk3',chunk);
})
readStream4.on('data', function(chunk){
console.log('en cours')
chunks4.push(chunk);
socket.emit('img-chunk4',chunk);
})
readStreamQR.on('data', function(chunk){
chunksQR.push(chunk);
socket.emit('QR-chunk',chunk);
})
readStreamQR2.on('data', function(chunk){
chunksQR2.push(chunk);
socket.emit('QR-chunk2',chunk);
})
readStreamQR3.on('data', function(chunk){
chunksQR3.push(chunk);
socket.emit('QR-chunk3',chunk);
})
readStreamQR4.on('data', function(chunk){
chunksQR4.push(chunk);
socket.emit('QR-chunk4',chunk);
})
readStream.on('end', function(){
console.log('Image 1 loaded');
})
readStream2.on('end', function(){
console.log('Image 2 loaded');
})
readStream3.on('end', function(){
console.log('Image 3 loaded');
})
readStream4.on('end', function(){
console.log('Image 4 loaded');
})
readStreamQR.on('end', function(){
console.log('QR 1 loaded');
})
readStreamQR2.on('end', function(){
console.log('QR 2 loaded');
})
readStreamQR3.on('end', function(){
console.log('QR 3 loaded');
})
readStreamQR4.on('end', function(){
console.log('QR 4 loaded');
})
});
//------------------------------------------------------------//
console.log("incoming socket connection");
// first send the history to the new client
for (var i in line_history) {
socket.emit('draw_line', { line: line_history[i] } );
}
// add handler for message type "draw_line".
socket.on('draw_line', function (data) {
// add received line to history
line_history.push(data.line);
// send line to all clients
io.emit('draw_line', { line: data.line });
});
// add handler to reset the drawing.
socket.on('reset_line', function (data) {
line_history = [];
// send line to all clients
io.emit('reset_line');
});
});
return io;
};