|
| 1 | +const app = require('express')(); |
| 2 | +const server = require('http').Server(app); |
| 3 | +const io = require('socket.io')(server); |
| 4 | +const port = 3000; |
| 5 | + |
| 6 | +server.listen(port, () => { |
| 7 | + console.log(`Server is running on port ${port}`); |
| 8 | +}); |
| 9 | + |
| 10 | +app.get('/', (req, res) => { |
| 11 | + res.sendFile(__dirname + '/public/index.html'); |
| 12 | +}); |
| 13 | + |
| 14 | +app.get('/javascript', (req, res) => { |
| 15 | + res.sendFile(__dirname + '/public/javascript.html'); |
| 16 | +}); |
| 17 | + |
| 18 | +app.get('/html', (req, res) => { |
| 19 | + res.sendFile(__dirname + '/public/html.html'); |
| 20 | +}); |
| 21 | + |
| 22 | +app.get('/css', (req, res) => { |
| 23 | + res.sendFile(__dirname + '/public/css.html'); |
| 24 | +}); |
| 25 | + |
| 26 | +app.get('/py', (req, res) => { |
| 27 | + res.sendFile(__dirname + '/public/py.html'); |
| 28 | +}); |
| 29 | + |
| 30 | +app.get('/react', (req, res) => { |
| 31 | + res.sendFile(__dirname + '/public/react.html'); |
| 32 | +}); |
| 33 | + |
| 34 | +//Create a namespace of different rooms |
| 35 | +const tech = io.of('/tech'); |
| 36 | + |
| 37 | + |
| 38 | +tech.on('connection', (socket) => { |
| 39 | + socket.on('join', (data) => { |
| 40 | + socket.join(data.room); |
| 41 | + tech.in(data.room).emit('message', `New user joined the ${data.room} room`) |
| 42 | + }) |
| 43 | + socket.on('message', (data) => { |
| 44 | + console.log(`message: ${data.msg}`); |
| 45 | + tech.in(data.room).emit('message', data.msg); |
| 46 | + }); |
| 47 | + |
| 48 | + socket.on('disconnect', () => { |
| 49 | + console.log('user disconnected'); |
| 50 | + |
| 51 | + tech.emit('message', 'user disconnected'); |
| 52 | + }) |
| 53 | +}) |
0 commit comments