我無法理解這部分使用c.conn.SetWriteDeadline函數(shù)的代碼。 // Time allowed to write a message to the peer. writeWait = 10 * time.Second // Time allowed to read the next pong message from the peer. pongWait = 60 * time.Second // Send pings to peer with this period. Must be less than pongWait. pingPeriod = (pongWait * 9) / 10// A goroutine running writePump is started for each connection. The// application ensures that there is at most one writer to a connection by// executing all writes from this goroutine.func (c *Client) writePump() { ticker := time.NewTicker(pingPeriod) defer func() { ticker.Stop() c.conn.Close() }() for { select { case message, ok := <-c.send: // LOGIC TO WRITE MESSAGE TO THE CONNECTION case <-ticker.C: c.conn.SetWriteDeadline(time.Now().Add(writeWait)) // ?? ?? if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil { return } } }}我無法理解票務(wù)渠道邏輯。我們想在這里實現(xiàn)什么?我指的是go/gorilla/websocket 這里的官方碼頭
1 回答

慕雪6442864
TA貢獻(xiàn)1812條經(jīng)驗 獲得超5個贊
來自 SetWriteDeadline 函數(shù)文檔:
SetWriteDeadline sets the write deadline on the underlying network connection.
After a write has timed out, the websocket state is corrupt and
all future writes will return an error.
A zero value for t means writes will not time out.
因此,goroutine 上的每個自動收報機都將寫入截止日期設(shè)置為當(dāng)前時間加上您將 writeWait const 設(shè)置為的任何內(nèi)容。隨后的行然后發(fā)送一個帶有該截止日期的 ping 消息。這一切都發(fā)生在代碼間隔,即 const pingPeriod。另一個 go-routine,readPump(),應(yīng)該為 pong 消息設(shè)置讀取截止日期和處理程序。根據(jù)您設(shè)置的等待時間,這種持續(xù)的乒乓球使連接保持活動狀態(tài)。
- 1 回答
- 0 關(guān)注
- 218 瀏覽
添加回答
舉報
0/150
提交
取消