MCPcopy
hub / github.com/wavetermdev/waveterm / WriteLoop

Function WriteLoop

pkg/web/ws.go:179–220  ·  view source on GitHub ↗
(conn *websocket.Conn, outputCh chan any, closeCh chan any, routeId string)

Source from the content-addressed store, hash-verified

177}
178
179func WriteLoop(conn *websocket.Conn, outputCh chan any, closeCh chan any, routeId string) {
180 ticker := time.NewTicker(wsInitialPingTime)
181 defer ticker.Stop()
182 initialPing := true
183 for {
184 select {
185 case msg := <-outputCh:
186 var barr []byte
187 var err error
188 if _, ok := msg.([]byte); ok {
189 barr = msg.([]byte)
190 } else {
191 barr, err = json.Marshal(msg)
192 if err != nil {
193 log.Printf("[websocket] cannot marshal websocket message: %v\n", err)
194 // just loop again
195 break
196 }
197 }
198 err = conn.WriteMessage(websocket.TextMessage, barr)
199 if err != nil {
200 conn.Close()
201 log.Printf("[websocket] WritePump error (%s): %v\n", routeId, err)
202 return
203 }
204
205 case <-ticker.C:
206 err := WritePing(conn)
207 if err != nil {
208 log.Printf("[websocket] WritePump error (%s): %v\n", routeId, err)
209 return
210 }
211 if initialPing {
212 initialPing = false
213 ticker.Reset(wsPingPeriodTickTime)
214 }
215
216 case <-closeCh:
217 return
218 }
219 }
220}
221
222func registerConn(wsConnId string, stableId string, wproxy *wshutil.WshRpcProxy) {
223 GlobalLock.Lock()

Callers 1

HandleWsInternalFunction · 0.85

Calls 3

WritePingFunction · 0.85
StopMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected