ruby - Detect when user leaves an event stream -
i'm trying make chatroom scratch using sinatra. need detect when user leaves page. @ first, thinking using javascript onbeforeunload
function, users fake leaving or leave without notifying server.
my code stream part looks this:
get '/stream', :provides => 'text/event-stream' stream :keep_open |out| connections << out end end
according 1 of sinatra example files, chat.rb
, makes chatroom, use
out.callback connections.delete(out) end
but in test, didn't fire when closed page test (in example, had puts
console if code fired, , nothing outputted console).
is there more reliable way detect if user leaves stream?
also, i'm not using socket.io (client-side) or anything, i'm open if solves problem.
based on chat.rb example, create small protocol exchange data, instead of clean chat messages.
with approach, can send "alive signal" each connection , clean doesn't respond.
i adapted little chat.rb illustrate idea: https://gist.github.com/tlewin/5708745
Comments
Post a Comment