Add websockets messages until its connected
This commit is contained in:
parent
0214741345
commit
a4a52e7ed4
1 changed files with 15 additions and 1 deletions
|
@ -16,6 +16,8 @@ class ZeroWebsocket
|
||||||
@ws.onopen = @onOpenWebsocket
|
@ws.onopen = @onOpenWebsocket
|
||||||
@ws.onerror = @onErrorWebsocket
|
@ws.onerror = @onErrorWebsocket
|
||||||
@ws.onclose = @onCloseWebsocket
|
@ws.onclose = @onCloseWebsocket
|
||||||
|
@connected = false
|
||||||
|
@message_queue = []
|
||||||
|
|
||||||
|
|
||||||
onMessage: (e) =>
|
onMessage: (e) =>
|
||||||
|
@ -47,7 +49,11 @@ class ZeroWebsocket
|
||||||
if not message.id?
|
if not message.id?
|
||||||
message.id = @next_message_id
|
message.id = @next_message_id
|
||||||
@next_message_id += 1
|
@next_message_id += 1
|
||||||
|
if @connected
|
||||||
@ws.send(JSON.stringify(message))
|
@ws.send(JSON.stringify(message))
|
||||||
|
else
|
||||||
|
@log "Not connected, adding message to queue"
|
||||||
|
@message_queue.push(message)
|
||||||
if cb
|
if cb
|
||||||
@waiting_cb[message.id] = cb
|
@waiting_cb[message.id] = cb
|
||||||
|
|
||||||
|
@ -58,6 +64,13 @@ class ZeroWebsocket
|
||||||
|
|
||||||
onOpenWebsocket: (e) =>
|
onOpenWebsocket: (e) =>
|
||||||
@log "Open"
|
@log "Open"
|
||||||
|
@connected = true
|
||||||
|
|
||||||
|
# Process messages sent before websocket opened
|
||||||
|
for message in @message_queue
|
||||||
|
@ws.send(JSON.stringify(message))
|
||||||
|
@message_queue = []
|
||||||
|
|
||||||
if @onOpen? then @onOpen(e)
|
if @onOpen? then @onOpen(e)
|
||||||
|
|
||||||
|
|
||||||
|
@ -68,6 +81,7 @@ class ZeroWebsocket
|
||||||
|
|
||||||
onCloseWebsocket: (e, reconnect=10000) =>
|
onCloseWebsocket: (e, reconnect=10000) =>
|
||||||
@log "Closed", e
|
@log "Closed", e
|
||||||
|
@connected = false
|
||||||
if e and e.code == 1000 and e.wasClean == false
|
if e and e.code == 1000 and e.wasClean == false
|
||||||
@log "Server error, please reload the page", e.wasClean
|
@log "Server error, please reload the page", e.wasClean
|
||||||
else # Connection error
|
else # Connection error
|
||||||
|
|
Loading…
Reference in a new issue