MCPcopy Index your code
hub / github.com/screego/server / startWriteHandler

Method startWriteHandler

ws/client.go:134–171  ·  view source on GitHub ↗

startWriteHandler starts the write loop. The method has the following tasks: * ping the client in the interval provided as parameter * write messages send by the channel to the client * on errors exit the loop.

(pingPeriod time.Duration)

Source from the content-addressed store, hash-verified

132// * write messages send by the channel to the client
133// * on errors exit the loop.
134func (c *Client) startWriteHandler(pingPeriod time.Duration) {
135 pingTicker := time.NewTicker(pingPeriod)
136 defer pingTicker.Stop()
137 defer func() {
138 c.debug().Msg("WebSocket Done")
139 }()
140 defer c.conn.Close()
141 for {
142 select {
143 case message := <-c.info.Write:
144 if msg, ok := message.(outgoing.CloseWriter); ok {
145 c.debug().Str("reason", msg.Reason).Int("code", msg.Code).Msg("WebSocket Close")
146 c.CloseOnDone(msg.Code, msg.Reason)
147 return
148 }
149
150 _ = c.conn.SetWriteDeadline(time.Now().Add(writeWait))
151 typed, err := ToTypedOutgoing(message)
152 c.debug().Interface("event", typed.Type).Interface("payload", typed.Payload).Msg("WebSocket Send")
153 if err != nil {
154 c.debug().Err(err).Msg("could not get typed message, exiting connection.")
155 c.CloseOnError(websocket.CloseNormalClosure, "malformed outgoing "+err.Error())
156 continue
157 }
158
159 if err := writeJSON(c.conn, typed); err != nil {
160 c.printWebSocketError("write", err)
161 c.CloseOnError(websocket.CloseNormalClosure, "write error"+err.Error())
162 }
163 case <-pingTicker.C:
164 _ = c.conn.SetWriteDeadline(time.Now().Add(writeWait))
165 if err := ping(c.conn); err != nil {
166 c.printWebSocketError("ping", err)
167 c.CloseOnError(websocket.CloseNormalClosure, "ping timeout")
168 }
169 }
170 }
171}
172
173func (c *Client) debug() *zerolog.Event {
174 return log.Debug().Str("id", c.info.ID.String()).Str("ip", c.info.Addr.String())

Callers 1

UpgradeMethod · 0.80

Calls 5

debugMethod · 0.95
CloseOnDoneMethod · 0.95
CloseOnErrorMethod · 0.95
printWebSocketErrorMethod · 0.95
ToTypedOutgoingFunction · 0.85

Tested by

no test coverage detected