Added ping connection feature.
This commit is contained in:
parent
cfd0e5be07
commit
3e71b4e484
2 changed files with 39 additions and 3 deletions
|
@ -7,6 +7,10 @@ class App.WebSocket
|
||||||
_instance ?= new _Singleton
|
_instance ?= new _Singleton
|
||||||
_instance
|
_instance
|
||||||
|
|
||||||
|
@close: (args) -> # Must be a static method
|
||||||
|
if _instance isnt undefined
|
||||||
|
_instance.close()
|
||||||
|
|
||||||
@send: (args) -> # Must be a static method
|
@send: (args) -> # Must be a static method
|
||||||
@connect()
|
@connect()
|
||||||
_instance.send(args)
|
_instance.send(args)
|
||||||
|
@ -48,6 +52,24 @@ class _Singleton extends Spine.Controller
|
||||||
close: =>
|
close: =>
|
||||||
@ws.close()
|
@ws.close()
|
||||||
|
|
||||||
|
ping: =>
|
||||||
|
console.log 'send websockend ping'
|
||||||
|
@send( { action: 'ping' } )
|
||||||
|
|
||||||
|
# check if ping is back within 30 sec.
|
||||||
|
if @check_id
|
||||||
|
clearTimeout(@check_id)
|
||||||
|
check = =>
|
||||||
|
console.log 'no websockend ping response, reconnect...'
|
||||||
|
@close()
|
||||||
|
@check_id = @delay check, 60000
|
||||||
|
|
||||||
|
pong: ->
|
||||||
|
console.log 'received websockend ping'
|
||||||
|
|
||||||
|
# test again after 10 sec.
|
||||||
|
@delay @ping, 30000
|
||||||
|
|
||||||
connect: =>
|
connect: =>
|
||||||
# console.log '------------ws connect....--------------'
|
# console.log '------------ws connect....--------------'
|
||||||
|
|
||||||
|
@ -80,13 +102,20 @@ class _Singleton extends Spine.Controller
|
||||||
@send(item)
|
@send(item)
|
||||||
@queue = []
|
@queue = []
|
||||||
|
|
||||||
@ws.onmessage = (e) ->
|
# send ping to check connection
|
||||||
|
@delay @ping, 30000
|
||||||
|
|
||||||
|
@ws.onmessage = (e) =>
|
||||||
pipe = JSON.parse( e.data )
|
pipe = JSON.parse( e.data )
|
||||||
console.log( "ws:onmessage", pipe )
|
console.log( "ws:onmessage", pipe )
|
||||||
|
|
||||||
# go through all blocks
|
# go through all blocks
|
||||||
for item in pipe
|
for item in pipe
|
||||||
|
|
||||||
|
# reset reconnect loop
|
||||||
|
if item['action'] is 'pong'
|
||||||
|
@pong()
|
||||||
|
|
||||||
# fill collection
|
# fill collection
|
||||||
if item['collection']
|
if item['collection']
|
||||||
console.log( "ws:onmessage collection:" + item['collection'] )
|
console.log( "ws:onmessage collection:" + item['collection'] )
|
||||||
|
|
|
@ -60,9 +60,11 @@ EventMachine.run {
|
||||||
client_id = ws.object_id
|
client_id = ws.object_id
|
||||||
puts 'Client ' + client_id.to_s + ' disconnected'
|
puts 'Client ' + client_id.to_s + ' disconnected'
|
||||||
|
|
||||||
|
# removed from current client list
|
||||||
if @clients.include? client_id
|
if @clients.include? client_id
|
||||||
@clients.delete client_id
|
@clients.delete client_id
|
||||||
end
|
end
|
||||||
|
|
||||||
Session.destory( client_id )
|
Session.destory( client_id )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,6 +79,11 @@ EventMachine.run {
|
||||||
if data['action'] == 'login'
|
if data['action'] == 'login'
|
||||||
@clients[client_id][:session] = data['session']
|
@clients[client_id][:session] = data['session']
|
||||||
Session.create( client_id, data['session'] )
|
Session.create( client_id, data['session'] )
|
||||||
|
|
||||||
|
# ping
|
||||||
|
elsif data['action'] == 'ping'
|
||||||
|
@clients[client_id][:last_ping] = Time.now
|
||||||
|
ws.send( '[{"action":"pong"}]' )
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue