-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
36 lines (23 loc) · 724 Bytes
/
Copy pathserver.js
File metadata and controls
36 lines (23 loc) · 724 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
import express from 'express'
import http, { createServer } from 'http'
import { Server } from 'socket.io'
const app = express()
const server = http.createServer(app)
app.use(express.static('client'))
const io = new Server(server);
server.listen(3000);
const players = {}
io.on('connect', (socket) => {
console.log('connesso ' + socket.handshake.address)
players[socket.id] = socket;
for(let k in players) socket.emit('spawn', { id: k })
socket.broadcast.emit('spawn', { id: socket.id })
socket.on('move', (data) => {
data.id = socket.id;
socket.broadcast.emit('update', data)
})
socket.on('disconnect', () => {
socket.broadcast.emit('despawn', { id: socket.id })
delete players[socket.id]
})
})