OK, this is a continuation of this topic: How to run the official Haskell WebSockets library example . I want a server that will respond to every client request in a loop.
{-# LANGUAGE OverloadedStrings #-} import Data.Char (isPunctuation, isSpace) import Data.Monoid (mappend) import Data.Text (Text) import Control.Exception (fromException) import Control.Monad (forM_, forever) import Control.Concurrent (MVar, newMVar, modifyMVar_, readMVar) import Control.Monad.IO.Class (liftIO) import qualified Data.Text as T import qualified Data.Text.IO as T import Network.WebSockets meow :: TextProtocol p => WebSockets p () meow = forever $ do msg <- receiveData sendTextData $ msg `T.append` ", meow." app :: Request -> WebSockets Hybi00 () app _ = meow main :: IO () main = runServer "0.0.0.0" 8000 app
Now I'm trying to use it from JavaScript:
var socket; var host = "ws://localhost:8000"; var socket = new WebSocket(host); console.log("ready"); socket.onopen = function(){ console.log("open"); socket.send("cats do "); } socket.onmessage = function(msg){ console.log("msg"); } socket.onclose = function(){ console.log("close"); }
But the connection closes immediately after the client starts (the connection is ready, but not open). I was expecting something else ...
source share