Reputation: 2940
I'd like to limit the connections to the websocket server. Namely to 1. The new client kicks the old client out.
This somewhat represents what I want to do. Taking what is in messages
and sending it through the websocket. If another client connects or the browser refreshes (which should close the old connection, but doesn't for some reason) there are suddenly 2 connections and only every second message receives at the new client.
I use the snap framework for this.
createServer = forkIO $ httpServe defaultConfig app
app = route [("/", runWebSocketsSnap handler)]
handler pending = do
connection <- acceptRequest pending
loop connection
loop connection = do
msg <- takeMVar messages
sendTextData connection msg
{-# NOINLINE messages #-}
messages = unsafePerformIO newEmptyMVar
sendMessage = putMVar messages
Upvotes: 0
Views: 91
Reputation: 13876
I see two different questions here:
how to limit number of connections, so there is at most N clients at the same time;
make sure old connection will not live forever after browser refresh;
I think you mean #2. In that case you should check that the connection is alive. The best way to do that is to ping the client periodically, e.g. using forkPingThread.
If you really need #1, then you should establish shared MVar
with ThreadId
of the current client in it. When new client connects, just kill the old one.
Upvotes: 1