-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
35 lines (26 loc) · 867 Bytes
/
server.js
File metadata and controls
35 lines (26 loc) · 867 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
const express = require('express')
const app = require('express')()
const http = require('http').Server(app)
const io = require('socket.io')(http)
const port = process.env.PORT || 80
app.use(express.static('public'))
// Socket IO Logic
let players = []
io.on('connection', socket => {
io.emit('join', { id: socket.id })
players.push(socket.id)
io.emit('players', { for: socket.id, players })
console.log("Player add", players)
socket.on('pos', msg => {
console.log(msg)
io.emit('pos', msg)
})
socket.on('disconnect', reason => {
players = players.filter(id => id !== socket.id)
io.emit('players', { for: "all", id: socket.id })
console.log(`Player leave for ${reason}`, players)
})
})
http.listen(port, () => {
console.log(`Socket.IO server running at http://localhost:${port}/`)
})