作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以写这样的东西吗
let echo (ws: WebSocket) =
fun ctx -> socket {
let loop = ref true
while !loop do
let! message = Async.Choose (ws.read()) (inbox.Receive())
match message with
| Choice1Of2 (wsMessage) ->
match wsMessage with
| Ping, _, _ -> do! ws.send Pong [||] true
| _ -> ()
| Choice2Of2 pushMessage -> do! ws.send Text pushMessage true
}
最佳答案
我认为您可以使用Async.Choose
解决此问题(有很多实现-尽管我不确定最规范的实现在哪里)。
也就是说,您当然可以创建两个循环-在socket { .. }
中读取一个循环,以便您可以从Web套接字接收数据。写一个可以是普通的async { ... }
块。
这样的事情应该可以解决问题:
let echo (ws: WebSocket) =
// Loop that waits for the agent and writes to web socket
let notifyLoop = async {
while true do
let! msg = inbox.Receive()
do! ws.send Text msg }
// Start this using cancellation token, so that you can stop it later
let cts = new CancellationTokenSource()
Async.Start(notifyLoop, cts.Token)
// The loop that reads data from the web socket
fun ctx -> socket {
let loop = ref true
while !loop do
let! message = ws.read()
match message with
| Ping, _, _ -> do! ws.send Pong [||] true
| _ -> () }
关于websocket - 如何在suave中通过websocket实现服务器推送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33030792/
我是一名优秀的程序员,十分优秀!