-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
73 lines (54 loc) · 2.16 KB
/
client.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
//socket.io-stream
var io = require('socket.io').listen(5050);
var ss = require('socket.io-stream');
var path = require('path');
const fs = require("fs-extra")
const player = require('node-wav-player');
io.on('connection', function (socket) {
console.log("somebody connected!");
ss(socket).on('get_location', function(stream, d){
console.log("received get_location request");
fs.readFile('python/pythonShit/config/locationLog.json', function(err, data){
obj = JSON.parse(data);
console.log("sending last location");
console.log(obj.locations[0].location);
// console.log(socket);
socket.emit("send_location", obj.locations[0].location)
})
})
ss(socket).on('new_picture', function (stream, data) {
var filename = data.name
stream.pipe(fs.createWriteStream("rsc/pictures/" + filename));
stream.on('end', function () {
console.log("Received new picture");
});
})
ss(socket).on('new_user_json', function (stream, data) {
var filename = data.name
stream.pipe(fs.createWriteStream('useless.txt'))
data.data.filename = "../rsc/pictures/"+ data.data.filename;
fs.readFile('rsc/users.json', function (err, d) {
var json = JSON.parse(d)
json.push(data.data)
fs.writeFile("rsc/users.json", JSON.stringify(json,null,4)).then(() =>{
console.log("updated user.json");
})
})
})
ss(socket).on('play-audio', function (stream, data) {
var filename = data.name
console.log(filename);
stream.pipe(fs.createWriteStream("voice_message/" + filename));
stream.on('end', function () {
console.log("received new audio file");
console.log("now playing ");
player.play({
path: "./client/"+filename,
}).then(() => {
console.log('The wav file started to be played successfully.');
}).catch((error) => {
console.error(error);
});
});
})
});