MCPcopy
hub / github.com/Terry-Mao/goim / dispatchWebsocket

Method dispatchWebsocket

internal/comet/server_websocket.go:306–405  ·  view source on GitHub ↗

dispatch accepts connections on the listener and serves requests for each incoming connection. dispatch blocks; the caller typically invokes it in a go statement.

(ws *websocket.Conn, wp *bytes.Pool, wb *bytes.Buffer, ch *Channel)

Source from the content-addressed store, hash-verified

304// for each incoming connection. dispatch blocks; the caller typically
305// invokes it in a go statement.
306func (s *Server) dispatchWebsocket(ws *websocket.Conn, wp *bytes.Pool, wb *bytes.Buffer, ch *Channel) {
307 var (
308 err error
309 finish bool
310 online int32
311 white = whitelist.Contains(ch.Mid)
312 )
313 if conf.Conf.Debug {
314 log.Infof("key: %s start dispatch tcp goroutine", ch.Key)
315 }
316 for {
317 if white {
318 whitelist.Printf("key: %s wait proto ready\n", ch.Key)
319 }
320 var p = ch.Ready()
321 if white {
322 whitelist.Printf("key: %s proto ready\n", ch.Key)
323 }
324 if conf.Conf.Debug {
325 log.Infof("key:%s dispatch msg:%s", ch.Key, p.Body)
326 }
327 switch p {
328 case grpc.ProtoFinish:
329 if white {
330 whitelist.Printf("key: %s receive proto finish\n", ch.Key)
331 }
332 if conf.Conf.Debug {
333 log.Infof("key: %s wakeup exit dispatch goroutine", ch.Key)
334 }
335 finish = true
336 goto failed
337 case grpc.ProtoReady:
338 // fetch message from svrbox(client send)
339 for {
340 if p, err = ch.CliProto.Get(); err != nil {
341 break
342 }
343 if white {
344 whitelist.Printf("key: %s start write client proto%v\n", ch.Key, p)
345 }
346 if p.Op == grpc.OpHeartbeatReply {
347 if ch.Room != nil {
348 online = ch.Room.OnlineNum()
349 }
350 if err = p.WriteWebsocketHeart(ws, online); err != nil {
351 goto failed
352 }
353 } else {
354 if err = p.WriteWebsocket(ws); err != nil {
355 goto failed
356 }
357 }
358 if white {
359 whitelist.Printf("key: %s write client proto%v\n", ch.Key, p)
360 }
361 p.Body = nil // avoid memory leak
362 ch.CliProto.GetAdv()
363 }

Callers 1

ServeWebsocketMethod · 0.95

Calls 11

ContainsMethod · 0.80
PrintfMethod · 0.80
ReadyMethod · 0.80
OnlineNumMethod · 0.80
WriteWebsocketHeartMethod · 0.80
WriteWebsocketMethod · 0.80
GetAdvMethod · 0.80
CloseMethod · 0.65
GetMethod · 0.45
FlushMethod · 0.45
PutMethod · 0.45

Tested by

no test coverage detected