-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
142 lines (128 loc) · 3.39 KB
/
app.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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const session = require('express-session');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 8080;
app.use(express.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors());
app.use(
session({
secret: 'temporary secret',
cookie: { maxAge: 24 * 60 * 60 * 1000, secure: false },
resave: false,
saveUninitialized: false,
}),
);
app.use(function (req, res, next) {
res.header(
'Access-Control-Allow-Origin',
'http://linserv1.cims.nyu.edu:23204',
// 'http://localhost:3001',
); // update to match the domain you will make the request from
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept',
);
next();
});
const registerRoute = require('./routes/register');
const loginRoute = require('./routes/login');
const userRoute = require('./routes/user');
app.use('/register', registerRoute);
app.use('/login', loginRoute);
app.use('/user', userRoute);
const server = app.listen(port, () => {
console.log(`server running on ${port}`);
});
const io = require('socket.io')(server);
let names = [];
io.on('connection', (socket) => {
console.log('a user connected');
console.log(names);
let hasSign = false;
let index = null;
//Client sends a new message
socket.on('new message', function (data) {
// we tell the client to execute 'new message'
const text = data[0];
const user = data[1];
io.emit('new message', {
name: user.name,
img: user.img,
text: text,
sign: user.sign,
timestamp: Date.now(),
});
});
//Client sends a new gif
socket.on('new gif', function (data) {
// we tell the client to execute 'new message'
const gif = data[0];
const user = data[1];
io.emit('new message', {
gif: gif,
name: user.name,
img: user.img,
sign: user.sign,
timestamp: Date.now(),
});
});
//Client sends a sign request
socket.on('request sign', function (input) {
const sign = input[0];
const name = input[1];
const img = input[2];
if (hasSign) return;
if (names.indexOf(name) == -1) {
//Store the sign in the socket
socket.sign = sign;
socket.name = name;
socket.img = img;
names.push(name);
console.log(names);
hasSign = true;
socket.emit('connected', {
sign: sign,
name: name,
img: img,
});
//Broadcast connected user message
io.emit('new message', {
sign: sign,
name: name,
img: img,
text: `${name} (${sign}) has connected`,
timestamp: Date.now(),
});
} else {
socket.emit('socket in use');
console.log('inuse');
}
});
//Handle a user disconnect
socket.on('disconnect', function () {
if (hasSign) {
let name;
names = names.filter((ele) => {
if (ele !== socket.name) {
return ele;
} else {
name = ele;
}
});
// const name = names.splice(index, 1);
console.log(name, 'log out');
console.log(names);
//Broadcast disconnect message
io.emit('new message', {
sign: socket.sign,
img: socket.img,
text: `${socket.name} (${socket.sign}) has disconnected`,
timestamp: Date.now(),
});
}
});
});