func Dotings(tcpConn *net.TCPConn) {
data := make([]byte, 128)
for {
//读取数据
res, _ := tcpConn.Read(data)
str := string(data[:res])
fmt.Println(str)
messnager := make(chan byte)
//检测每次Client是否有数据传来
go GravelChannel(data[:res], messnager)
//心跳计时
go HeartBeating(tcpConn, messnager)
}
}
func HeartBeating(tcpConn *net.TCPConn, readerChannel chan byte) {
select {
case <-time.After(time.Second * 5):
tcpConn.Close()
case <-readerChannel:
break
}
}
func GravelChannel(res []byte, mess chan byte) {
for _, v := range res {
mess <- v
}
close(mess)
}
按道理是如果经过5秒,客户端没有发来消息就会关闭连接,但当Client没有发消息时,感觉会阻塞到 res, err := tcpConn.Read(data) 这块。不能执行 go GravelChannel(data[:res], messnager) 和 go HeartBeating(tcpConn, messnager)
(求大神指点)