-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·81 lines (70 loc) · 2.26 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
const express = require("express");
const bodyParser = require("body-parser");
const server = express();
const socket = require("socket.io");
const ss = require("socket.io-stream");
const http = require("http");
const path = require("path");
server.use("/", express.static("public"));
server.use(bodyParser.json());
let httpServer = http.createServer(server);
httpServer.listen(8080, () => {
console.log("listening on 8080");
});
var io = socket(httpServer);
let arrayOfUsers = [];
let user;
io.sockets.on("connection", socket => {
console.log("we have a connection: " + socket.id);
socket.on("newUser", userData => {
console.log("server new user");
let user = {
id: socket.id,
username: userData.name,
files: userData.files
};
arrayOfUsers.push(user);
io.emit("updateUsers", arrayOfUsers);
console.log(arrayOfUsers);
});
socket.on("updateChangesToDesktop", updateData => {
const user = arrayOfUsers.find(user => user.id === updateData.id);
if (user) {
console.log("match");
user.files = updateData.files;
}
io.emit("updateUsers", arrayOfUsers);
});
//does this work? if/else statement
socket.on("fileTransferReq", data => {
console.log(data);
const user = arrayOfUsers.find(user => user.id === data.to);
//hvis user har filen som bliver sendt så afbryd
const dataFile = user.files.find(obj => data.file === obj);
if (dataFile) {
console.log("file exists");
} else {
user.files.push(data.file);
io.emit("updateUsers", arrayOfUsers);
let fromUser = data.from;
io.to(fromUser).emit("reqStreamFromUser", data);
}
});
ss(socket).on("streamToServer", (stream, data) => {
const outgoingstream = ss.createStream();
const userToReceiveData = data.receiver;
const connection = io.sockets.connected[userToReceiveData];
ss(connection).emit("fileStreamFromServer", outgoingstream, data);
stream.pipe(outgoingstream);
});
socket.on("disconnect", user => {
console.log("user disconnected" + socket.id);
for (let i = 0; i < arrayOfUsers.length; i++) {
if (arrayOfUsers[i].id == socket.id) {
arrayOfUsers.splice(i, 1);
}
console.log(arrayOfUsers);
io.sockets.emit("updateUsers", arrayOfUsers);
}
});
});