Skip to content

Commit

Permalink
pong实现
Browse files Browse the repository at this point in the history
  • Loading branch information
snowinszu committed Jan 13, 2021
1 parent 4c9bf06 commit f5226ed
Showing 1 changed file with 41 additions and 12 deletions.
53 changes: 41 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,24 +283,53 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
broadcastClient.BroadcastMsgLeave(id)
}
}()

msg := make([]wsutil.Message, 0, 4)
for {
msg, _, err := wsutil.ReadClientData(conn)
msg, err = wsutil.ReadClientMessage(conn, msg[:0])
if err != nil {
// handle error
//log.Printf("ReadClientData " + err.Error())
log.Infof("read message error: %v", err)
break
}
//log.Infof("ReadClientData from " + id)
msg = bytes.TrimSpace(bytes.Replace(msg, newline, space, -1))
hdr, err := handler.NewHandler(msg, c)
if err != nil {
// 心跳包
log.Infof("NewHandler " + err.Error())
} else {
hdr.Handle()
for _, m := range msg {
// ping
if m.OpCode.IsControl() {
err := wsutil.HandleClientControlMessage(conn, m)
if err != nil {
log.Infof("handle control error: %v", err)
}
continue
}
data := bytes.TrimSpace(bytes.Replace(m.Payload, newline, space, -1))
hdr, err := handler.NewHandler(data, c)
if err != nil {
// 心跳包
log.Infof("NewHandler " + err.Error())
} else {
hdr.Handle()
}
}
}

//for {
// msg, _, err := wsutil.ReadClientData(conn)
// if err != nil {
// // handle error
// //log.Printf("ReadClientData " + err.Error())
// break
// }
//

//
// //log.Infof("ReadClientData from " + id)
// msg = bytes.TrimSpace(bytes.Replace(msg, newline, space, -1))
// hdr, err := handler.NewHandler(msg, c)
// if err != nil {
// // 心跳包
// log.Infof("NewHandler " + err.Error())
// } else {
// hdr.Handle()
// }
//}
}()
}

Expand Down

0 comments on commit f5226ed

Please sign in to comment.