-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserver.js
More file actions
34 lines (26 loc) · 1.09 KB
/
server.js
File metadata and controls
34 lines (26 loc) · 1.09 KB
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 http = require('http')
const socketIO = require('socket.io')
// our localhost port
const port = 4001
const app = express()
// our server instance
const server = http.createServer(app)
// This creates our socket using the instance of the server
const io = socketIO(server)
// This is what the socket.io syntax is like, we will work this later
io.on('connection', socket => {
console.log('New client connected')
// just like on the client side, we have a socket.on method that takes a callback function
socket.on('change color', (color) => {
// once we get a 'change color' event from one of our clients, we will send it to the rest of the clients
// we make use of the socket.emit method again with the argument given to use from the callback function above
console.log('Color Changed to: ', color)
io.sockets.emit('change color', color)
})
// disconnect is fired when a client leaves the server
socket.on('disconnect', () => {
console.log('user disconnected')
})
})
server.listen(port, () => console.log(`Listening on port ${port}`))