MCPcopy Create free account
hub / github.com/LockGit/gochat / readPump

Method readPump

connect/server.go:90–155  ·  view source on GitHub ↗
(ch *Channel, c *Connect)

Source from the content-addressed store, hash-verified

88}
89
90func (s *Server) readPump(ch *Channel, c *Connect) {
91 defer func() {
92 logrus.Infof("start exec disConnect ...")
93 if ch.Room == nil || ch.userId == 0 {
94 logrus.Infof("roomId and userId eq 0")
95 ch.conn.Close()
96 return
97 }
98 logrus.Infof("exec disConnect ...")
99 disConnectRequest := new(proto.DisConnectRequest)
100 disConnectRequest.RoomId = ch.Room.Id
101 disConnectRequest.UserId = ch.userId
102 s.Bucket(ch.userId).DeleteChannel(ch)
103 if err := s.operator.DisConnect(disConnectRequest); err != nil {
104 logrus.Warnf("DisConnect err :%s", err.Error())
105 }
106 ch.conn.Close()
107 }()
108
109 ch.conn.SetReadLimit(s.Options.MaxMessageSize)
110 ch.conn.SetReadDeadline(time.Now().Add(s.Options.PongWait))
111 ch.conn.SetPongHandler(func(string) error {
112 ch.conn.SetReadDeadline(time.Now().Add(s.Options.PongWait))
113 return nil
114 })
115
116 for {
117 _, message, err := ch.conn.ReadMessage()
118 if err != nil {
119 if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
120 logrus.Errorf("readPump ReadMessage err:%s", err.Error())
121 return
122 }
123 }
124 if message == nil {
125 return
126 }
127 var connReq *proto.ConnectRequest
128 logrus.Infof("get a message :%s", message)
129 if err := json.Unmarshal([]byte(message), &connReq); err != nil {
130 logrus.Errorf("message struct %+v", connReq)
131 }
132 if connReq == nil || connReq.AuthToken == "" {
133 logrus.Errorf("s.operator.Connect no authToken")
134 return
135 }
136 connReq.ServerId = c.ServerId //config.Conf.Connect.ConnectWebsocket.ServerId
137 userId, err := s.operator.Connect(connReq)
138 if err != nil {
139 logrus.Errorf("s.operator.Connect error %s", err.Error())
140 return
141 }
142 if userId == 0 {
143 logrus.Error("Invalid AuthToken ,userId empty")
144 return
145 }
146 logrus.Infof("websocket rpc call return userId:%d,RoomId:%d", userId, connReq.RoomId)
147 b := s.Bucket(userId)

Callers 1

serveWsMethod · 0.80

Calls 6

BucketMethod · 0.95
AddMethod · 0.80
DisConnectMethod · 0.65
ConnectMethod · 0.65
DeleteChannelMethod · 0.45
PutMethod · 0.45

Tested by

no test coverage detected