Last active
November 24, 2015 19:54
-
-
Save skatenerd/2fee8c7956ffc0f46dfb to your computer and use it in GitHub Desktop.
Unexpected Connection Close
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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