-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
44 lines (34 loc) · 1.33 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
const express = require('express');
const http = require('http');
const socketIO = require('socket.io');
const path = require('path');
const app = express();
const server = http.createServer(app);
const io = socketIO(server);
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
io.on('connection', (socket) => {
socket.on('newuser', (user) => {
socket.username = user;
socket.emit('connection.success', ResponseMessage("Server", `Hello ${socket.username}, you have successfully connected`));
socket.broadcast.emit('connection.others', ResponseMessage("Server", `${socket.username} has connected`));
});
socket.on('send', (msg) => {
io.emit("SendClient", ResponseMessage(socket.username, msg));
});
socket.on('uploadImage', (msg) => {
io.emit("SendImage", ResponseMessage(socket.username, msg));
});
socket.on('disconnect', () => {
socket.broadcast.emit("connection.others", ResponseMessage("Server", `${socket.username} has left`));
});
});
function ResponseMessage(u, msg) {
return { UserName: u, Message: msg, Time: new Date().toTimeString() };
}
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`App started at port ${port}`);
});