Nhowka / Elmish.Bridge

Create client-server Fable-Elmish apps keeping a single mindset
MIT License
138 stars 17 forks source link

How to handle connections to different websocket servers? #56

Closed mrakgr closed 1 year ago

mrakgr commented 1 year ago
Program.mkProgram Index.init Index.update Index.view
|> Program.withSubscription (fun model ->
    let body msg msg_closed dispatch =
        let config =
            Bridge.endpoint $"{Url.learn_server}/{socket_endpoint}"
            |> Bridge.withWhenDown msg_closed
            |> Bridge.withMapping Index.FromServer

        Bridge.asSubscription config dispatch

        Bridge.Send // how do we use this?

        config :> IDisposable
    [
        for KeyValue(name,pl) in model.cfr_players do
            if pl.training_iterations_left > 0 then
                let key = [ string name; "train"; string pl.training_iterations_left ]
                key, body (FromClient (MsgClientToServer.Train (pl.training_iterations_left, pl.training_model)))
                        (Index.ConnectionClosed(name,true))
            if pl.testing_iterations_left > 0 then
                let key = [ string name; "test"; string pl.testing_iterations_left ]
                key, body (FromClient (MsgClientToServer.Test (pl.testing_iterations_left, pl.testing_model)))
                        (Index.ConnectionClosed(name,false))
    ]
    )
|> Program.withBridgeConfig (
        Bridge.endpoint socket_endpoint
        |> Bridge.withMapping Index.FromServer
        )
|> Program.withReactSynchronous "app"
#if DEBUG
|> Program.withDebugger
#endif
|> Program.run

It is really good that Elmish.Bridge has a way of closing and reacting to connections closings, but I want to be able to interact with multiple ws servers. I do not understand how Bridge.Send could be configured to send to a specific server.

Is that possible?

mrakgr commented 1 year ago
Bridge.Sender(msg,config.name,ignore,msg.GetType())

Ah, I see it has a function that could be used for that even though it doesn't show up in the editor.

mrakgr commented 1 year ago

Also, instead of the above, I just realized there is Bridge.NamedSend, sorry about that.