-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
44 lines (33 loc) · 1003 Bytes
/
Copy pathserver.js
File metadata and controls
44 lines (33 loc) · 1003 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
41
42
43
44
// Andre Assadi
// p5 IO Game
// File: server.js
const express = require("express")
const app = express()
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use(express.static('p5'))
app.get("/", (req,res) => {
res.render("game")
})
const port = process.env.PORT || 3000
var server = app.listen(port)
const io = require('socket.io')(server)
// listen on every connection
io.on('connection', (socket) => {
console.log('New user connected')
socket.emit('userid', { id : socket.id })
socket.on('new_user',(data) => {
io.sockets.emit('new_user',{id:socket.id, data:data})
})
// once disconnected
socket.on("disconnect", (data) => {
console.log(socket.id)
io.sockets.emit("broadcast_disconnect",{id: socket.id });
});
socket.on('initial_location',(data) => {
io.sockets.emit('initial_location',{id:socket.id, data:data})
})
socket.on('updated_location',(data) => {
io.sockets.emit('updated_location',{id: socket.id,xVel: data.xVel, yVel: data.yVel})
})
})