第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Go gorilla websocket 寫入截止日期

Go gorilla websocket 寫入截止日期

Go
Smart貓小萌 2022-12-05 17:30:38
我無法理解這部分使用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)。


查看完整回答
反對 回復(fù) 2022-12-05
  • 1 回答
  • 0 關(guān)注
  • 218 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號