大家好,简单问个 coder/websocket + Echo v5 的问题。
既然 Echo 会为每个处理函数单独运行一个 goroutine,我能直接让这个 goroutine 一直执行读取循环,而不额外加 go readPump() 吗?
func (endpoint *SocketEndpoint) ServeSocket(ctx echo.Context) error {
socket, _ := websocket.Accept(ctx.Response(), ctx.Request(), nil)
defer socket.CloseNow()
for {
_, message, readErr := socket.Read(ctx.Request().Context())
if readErr != nil { return nil }
endpoint.bus.Publish(message)
}
}
既然 coder/websocket 内部管理了并发写入,hub 是否也能直接使用 conn.Write(),不必另外开一个 writePump goroutine?这样安排对吗?
从英语翻译·查看原文Hey folks Brief question regarding coder/websocket + Echo v5:
Given that Echo executes every handler on a goroutine of its own, can I simply keep that goroutine occupied running the read loop — without adding go readPump()?
func (endpoint *SocketEndpoint) ServeSocket(ctx echo.Context) error {
socket, _ := websocket.Accept(ctx.Response(), ctx.Request(), nil)
defer socket.CloseNow()
for {
_, message, readErr := socket.Read(ctx.Request().Context())
if readErr != nil { return nil }
endpoint.bus.Publish(message)
}
}
Since concurrent writes are managed inside coder/websocket, can the hub also use conn.Write() itself rather than having an additional writePump goroutine — would that arrangement be correct?