-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
105 lines (82 loc) · 2.57 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const path = require('path');
const http = require('http');
const https = require('https');
const fs = require('fs');
const express = require('express');
var firebase = require('firebase');
const socketio = require('socket.io');
const WebSocket = require('ws');
const formatMessage = require('./utils/messages');
const {
userJoin,
getCurrentUser,
userLeave,
getRoomUsers
} = require('./utils/users');
const app = express();
//const server = https.createServer(app);
/*
var privateKey = fs.readFileSync('key.pem');
var certificate = fs.readFileSync('cert.pem');
var credentials = {key: privateKey, cert: certificate};
// your express configuration here
var httpServer = http.createServer(app);
var server = https.createServer(credentials, app);
//httpServer.listen(8080);
server.listen(3000);*/
const server = app.listen(process.env.PORT || 3000);
const io = socketio(server);
// Set static folder
app.use(express.static(path.join(__dirname, 'public')));
const botName = 'SquashCord Bot';
// Run when client connects
io.on('connection', socket => {
socket.on('joinRoom', ({ username, room }) => {
const user = userJoin(socket.id, username, room);
// Welcome current user
users = getRoomUsers(user.room);
socket.join(user.room);
socket.emit('message', formatMessage(botName, 'Welcome to SquashCord!'));
// Broadcast when a user connects
socket.broadcast
.to(user.room)
.emit(
'message',
formatMessage(botName, `${user.username} has joined the chat`)
);
// Send users and room info
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
});
socket.on('full_room',msg => {
socket.emit('room_full',msg);
});
// Listen for chatMessage
socket.on('chatMessage', msg => {
const user = getCurrentUser(socket.id);
io.to(user.room).emit('message', formatMessage(user.username, msg));
});
socket.on('videomessage',msg =>{
const user = getCurrentUser(socket.id);
socket.broadcast.to(user.room).emit('vmessage',msg);
});
// Runs when client disconnects
socket.on('disconnect', () => {
const user = userLeave(socket.id);
if (user) {
io.to(user.room).emit(
'message',
formatMessage(botName, `${user.username} has left the chat`)
);
// Send users and room info
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
}
});
});
//const PORT = process.env.PORT || 3000;
//server.listen(PORT, () => console.log(`Server running on port ${PORT}`));