-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
26 lines (23 loc) · 810 Bytes
/
index.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
module.exports = function(url = location.href.replace('http', 'ws')){
const io = emitterify({ attempt: 0 })
io.ready = io.once('connected')
io.connect = connect(io, url)
io.connect()
io.send = data => io.ready.then(socket => socket.send(data))
return io
}
const emitterify = require('utilise/emitterify')
, { min, pow } = Math
const connect = (io, url) => () => {
const { WebSocket, location, setTimeout } = window
, socket = new WebSocket(url)
socket.onopen = d => io.emit('connected', socket)
socket.onmessage = d => io.emit('recv', d.data)
socket.onclose = d => {
io.ready = io.once('connected')
io.emit('disconnected')
setTimeout(io.connect, backoff(++io.attempt))
}
}
const backoff = (attempt, base = 100, cap = 10000) =>
min(cap, base * pow(2, attempt))