I'm using a websocket (socket from dart:html
) and I wanted to know if I could send a message when the user closes the browser or exits the page.
I tried the onClose
event like this:
ws = new WebSocket('ws://$hostname:$port/ws');
ws.onOpen.listen(onSocketOpen);
ws.onClose.listen((e) {
ws.send("I disconnected");
});
but it won't send anything in these particular cases. Alternatively, on my server I tried this as well, but with the onDone
event :
WebSocketTransformer.upgrade(req)
..then((socket) => socket.listen((msg) => doSomething(), onDone:() => print("connection closed)));
That works, but I'd like to know what client disconnected (the reason why I wanted to send something from the actual disconnecting client).
Is there a reason to do that? I really don't feel like querying all other clients to know if they're still connected when someone disconnects...