-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
114 lines (85 loc) · 2.97 KB
/
main.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
const express = require('express');
const app = express();
const path = require('path');
const FileSaver = require('file-saver');
const fs = require('fs');
const http = require('http').Server(app)
const port = process.env.PORT || 5000;
// attach http server to the socket.io
const io = require('socket.io')(http);
let counterFile = 0;
// create a new connection
io.on('connection', socket => {
console.log(`A User Connected with ID = ${socket.id}`);
socket.on("message", msg => {
console.log("Client send " + msg);
})
//emit event
socket.emit("server", "recieve from server");
const users = {};
socket.on('new-user', name => {
users[socket.id] = name;
socket.broadcast.emit('user-connected', name);
})
socket.on('time-connect', time => {
console.log(`connected at ${time}`);
})
socket.on('send-chat-message', message => {
socket.broadcast.emit('chat-message', {message:message, name:users[socket.id]});
console.log(`${users[socket.id]}: ${message}`);
})
socket.on('send-time', time => {
console.log(`send at ${time}`);
})
socket.on('disconnect', () => {
socket.broadcast.emit('user-disconnected', users[socket.id]);
console.log(`User ${users[socket.id]} with ID (${socket.id}) disconnected`);
delete users[socket.id];
})
socket.on('time-disconnect', time => {
console.log(`disconnect at ${time}`);
})
socket.on('typing', (name) => {
socket.broadcast.emit('typing', name);
})
socket.on('send-vn', voiceData => {
socket.broadcast.emit("vn", {voiceData:voiceData, name:users[socket.id]});
// console.log(`${users[socket.id]}: ${voiceData}`);
fs.appendFile("./Audio/Audio" + users[socket.id] + (counterFile++) + ".txt", voiceData, function(err) {
if(err) {
return console.log(err);
}
console.log("Audio File Was Recieved!!");
});
})
socket.on('vn-record', name => {
socket.broadcast.emit('record-vn', name);
})
socket.on('send-file', fileData => {
socket.broadcast.emit("file-sending", {fileData:fileData, name:users[socket.id]});
// console.log(fileData);
fs.appendFile("./Images/Image" + users[socket.id] + (counterFile++) + ".txt", fileData, function(err) {
if(err) {
return console.log(err);
}
console.log("Image File Was Recieved!!");
});
})
})
app.use(function (request, result, next){
result.setHeader("Access-Control-Allow-Origin", "*");
next();
})
// route
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, "src/index.html"));
})
app.get('/get-style', (req, res) => {
res.sendFile(path.join(__dirname, "src/style.css"));
})
app.get('/get-script', (req, res) => {
res.sendFile(path.join(__dirname, "src/script.js"));
})
http.listen(port, () => {
console.log(`App Listening on port ${port}`)
})