我正在尝试从 websocket 流式传输 JSON 文本。但是,在初次阅读后,我注意到流似乎中断/断开连接。这是来自 Pleroma 服务器(想想:Mastodon)。我正在使用默认的 Golang websocket 库。
package main
import (
"bufio"
"fmt"
"log"
"golang.org/x/net/websocket"
)
func main() {
origin := "https://poa.st/"
url := "wss://poa.st/api/v1/streaming/?stream=public"
ws, err := websocket.Dial(url, "", origin)
if err != nil {
log.Fatal(err)
}
s := bufio.NewScanner(ws)
for s.Scan() {
line := s.Text()
fmt.Println(line)
}
}
在初始 JSON 文本响应之后,for 循环中断。我希望它每隔几秒钟发送一条新消息。
这可能是什么原因造成的?如果我可以使用 Gorilla websocket 库,我愿意切换到bufio.
谢谢!
饮歌长啸
相关分类