-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
45 lines (32 loc) · 920 Bytes
/
server.js
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
45
const geckos = require('@geckos.io/server').default
const { iceServers } = require('@geckos.io/server')
const FPS = 1000 / 60
const io = geckos({
iceServers: process.env.NODE_ENV === 'production' ? iceServers : iceServers
})
io.listen()
let updates = []
let start = new Date().getTime()
setInterval(() => {
const u = [...updates]
updates = []
const buffer = new ArrayBuffer(u.length * 8)
const view = new DataView(buffer)
for (let i = 0; i < u.length; i++) {
view.setInt32(i * 8, u[i].x)
view.setInt32(i * 8 + 4, u[i].y)
}
io.raw.emit(buffer)
let end = new Date().getTime()
const realFps = Math.round(FPS - (end - start))
if (realFps < -2) console.log(realFps)
start = end
}, FPS)
io.onConnection(channel => {
channel.onRaw(buffer => {
const view = new DataView(buffer)
const x = +view.getInt32(0)
const y = +view.getInt32(4)
updates.push({ x, y })
})
})