-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (27 loc) · 856 Bytes
/
index.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
const express = require('express');
const app = express();
const path = require('path');
//settings
const PORT = process.env.PORT || 3700;
//static files
app.use(express.static(path.join(__dirname, 'public')));
//start the server
const server = app.listen(PORT, () => {
console.log('Server is ready on port:', PORT);
});
//websockets
const Socketio = require('socket.io');
const io = Socketio(server);
io.on('connection', (socket)=>{
var countUsers = io.engine.clientsCount;
io.sockets.emit('users_online', countUsers);
socket.on('chat_message', (data) => {
socket.broadcast.emit('chat_message', data);
});
socket.on('chat_typing', (username) => {
socket.broadcast.emit('chat_typing', username);
});
socket.on('disconnect', () => {
io.sockets.emit('users_online', countUsers);
});
});