gpt4 book ai didi

haskell - 简单的 Haskell WebSockets 应用程序 - 连接在客户端启动后立即关闭(连接已准备好,但未打开)

转载 作者:行者123 更新时间:2023-12-02 14:08:56 25 4
gpt4 key购买 nike

好的,这是本主题的延续:How to run official example of Haskell WebSockets library 。我想要一个能够循环响应每个客户端请求的服务器。

{-# 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

现在我尝试从 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");
}

但是连接在客户端启动后立即关闭(连接已准备好,但未打开)。我期待着别的东西......

最佳答案

您需要accept the request在您可以开始使用 websocket 连接之前。例如

app :: Request -> WebSockets Hybi00 ()
app req = do
acceptRequest req
meow

关于haskell - 简单的 Haskell WebSockets 应用程序 - 连接在客户端启动后立即关闭(连接已准备好,但未打开),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14525437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com