发布到所有温文尔雅的websockets

时间:2016-06-30 11:16:29

标签: suave

如何在以下代码示例中向所有websocket客户端广播事件:

input:focus { ... }

所以,基本上,我希望能够向服务器websocket发送消息,并将该消息回显给所有当前的websocket客户端。用例是我有一个文件监视器,它向服务器websocket和浏览器中的websocket客户端发送消息,如果收到消息,它将刷新浏览器。

我想我需要保留一份客户名单,但我不确定最好的方法是什么样的。@ / p>

例如,使用express这很简单:

let echo (ws : WebSocket) =
    fun cx -> socket {
        let loop = ref true
        while !loop do
            let! msg = ws.read();
            match msg with
            | (Text, data, true) ->
                let str = UTF8.toString data
                printfn "****** Received: %s" str
                do! ws.send Text data true
            | (Ping, _, _) ->
                do! ws.send Pong [||] true
            | (Close, _, _) ->
                do! ws.send Close [||] true
                loop := false
            | _ -> ()
    }

let start home port =
    let config =
        { defaultConfig with
            logger = Logging.Loggers.saneDefaultsFor Logging.LogLevel.Verbose
            bindings = [ (if port |> String.IsNullOrEmpty then
                            HttpBinding.mkSimple HTTP "127.0.0.1" 3000
                          else HttpBinding.mkSimple HTTP "0.0.0.0" (int32 port)) ]
            homeFolder = home |> Some }

    let app =
        choose
            [
                path "/echo" >=> handShake echo
                NOT_FOUND "Sorry there is nothing there"
            ]

    startWebServer config app

0 个答案:

没有答案