MCPcopy
hub / github.com/gorilla/websocket / writePump

Method writePump

examples/chat/client.go:82–121  ·  view source on GitHub ↗

writePump pumps messages from the hub to the websocket connection. 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.

()

Source from the content-addressed store, hash-verified

80// application ensures that there is at most one writer to a connection by
81// executing all writes from this goroutine.
82func (c *Client) writePump() {
83 ticker := time.NewTicker(pingPeriod)
84 defer func() {
85 ticker.Stop()
86 c.conn.Close()
87 }()
88 for {
89 select {
90 case message, ok := <-c.send:
91 c.conn.SetWriteDeadline(time.Now().Add(writeWait))
92 if !ok {
93 // The hub closed the channel.
94 c.conn.WriteMessage(websocket.CloseMessage, []byte{})
95 return
96 }
97
98 w, err := c.conn.NextWriter(websocket.TextMessage)
99 if err != nil {
100 return
101 }
102 w.Write(message)
103
104 // Add queued chat messages to the current websocket message.
105 n := len(c.send)
106 for i := 0; i < n; i++ {
107 w.Write(newline)
108 w.Write(<-c.send)
109 }
110
111 if err := w.Close(); err != nil {
112 return
113 }
114 case <-ticker.C:
115 c.conn.SetWriteDeadline(time.Now().Add(writeWait))
116 if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
117 return
118 }
119 }
120 }
121}
122
123// serveWs handles websocket requests from the peer.
124func serveWs(hub *Hub, w http.ResponseWriter, r *http.Request) {

Callers 1

serveWsFunction · 0.95

Calls 5

WriteMessageMethod · 0.80
NextWriterMethod · 0.80
CloseMethod · 0.45
SetWriteDeadlineMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected