Skip to content

Commit

Permalink
yaaaaaaaaaaas
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed May 14, 2018
0 parents commit 3cd319f
Show file tree
Hide file tree
Showing 10 changed files with 10,032 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
node_modules/
.cache/
dist/
37 changes: 37 additions & 0 deletions api/index.js
@@ -0,0 +1,37 @@
const {Server} = require('ws')

const wss = new Server({port: 3714})

const channels = {}
const channelUsers = {}

wss.on('connection', (ws, request) => {
let channel

const handlers = {
channel (next) {
channel = next
channels[channel] = channels[channel] || {}
channelUsers[channel] = channelUsers[channel] || []
channelUsers[channel].push(ws)
ws.send(JSON.stringify({
type: 'positions',
positions: channels[channel]
}))
},
position (id, x, y, z) {
channels[channel][id] = {x, y, z}
channelUsers[channel].forEach(user => {
user.send(JSON.stringify({
type: 'position',
position: [id, x, y, z]
}))
})
}
}

ws.on('message', message => {
const [type, ...data] = JSON.parse(message)
handlers[type](...data)
})
})

0 comments on commit 3cd319f

Please sign in to comment.