-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
40 lines (31 loc) · 994 Bytes
/
Copy pathindex.js
File metadata and controls
40 lines (31 loc) · 994 Bytes
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
const {Server} = require("socket.io");
const http = require("http");
const express = require("express");
const app = express();
const server = http.createServer(app);
const io = new Server(server);
app.set('view engine', 'hbs');
app.use('/node', express.static('node_modules'));
app.use('/public', express.static('public'));
app.get('/', (req, res)=>{
res.render('index');
});
io.on('connection', (socket) =>{
// check user connected
console.log(`a user with id (${socket.id}) connected `);
socket.on('disconnect', ()=>{
console.log(`a user with id (${socket.id}) disconnected`);
});
socket.on('chat message', (msg)=>{
// CLIENT TO CLIENT
socket.broadcast.emit('chat message', msg);
});
socket.on('server chat message', (msg)=>{
// SERVER TO ALL CLIENTS
socket.broadcast.emit('server chat message', msg);
});
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});