Skip to content

Instantly share code, notes, and snippets.

@skatenerd
Last active November 24, 2015 19:54
Show Gist options
  • Save skatenerd/2fee8c7956ffc0f46dfb to your computer and use it in GitHub Desktop.
Save skatenerd/2fee8c7956ffc0f46dfb to your computer and use it in GitHub Desktop.
Unexpected Connection Close
module Main where
import qualified Data.Text as T
import qualified Network.WebSockets as WS
import qualified Control.Concurrent as C
import qualified Control.Monad as M
main :: IO ()
main = do
WS.runServer "0.0.0.0" 9160 acceptConnection
acceptConnection pending = do
conn <- WS.acceptRequest pending
WS.forkPingThread conn 30
bucket <- C.newMVar conn
C.forkIO $ sendPing bucket
return ()
sendPing bucket = do
C.threadDelay 100000
connection <- C.readMVar bucket
WS.sendTextData connection $ T.pack "HI"
<script type="text/javascript">
var connection = new WebSocket("ws://localhost:9160/?worldID=123");
var secondConnection = new WebSocket("ws://localhost:9160/?worldID=456");
connection.onclose = function(e) {
console.log(e.code)
};
connection.onmessage = function(message){
console.log(message.data);
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment