-
Notifications
You must be signed in to change notification settings - Fork 29
/
socket-chat-server.go
76 lines (71 loc) · 1.92 KB
/
socket-chat-server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//++++++++++++++++++++++++++++++++++++++++
// 《Go Web编程实战派从入门到精通》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 仓库地址:https://gitee.com/shirdonl/goWebActualCombat
// 仓库地址:https://github.com/shirdonl/goWebActualCombat
//++++++++++++++++++++++++++++++++++++++++
package main
import (
"fmt"
"net"
"time"
)
var ConnSlice map[net.Conn]*Heartbeat
type Heartbeat struct {
endTime int64 //过期时间
}
func main() {
ConnSlice = map[net.Conn]*Heartbeat{}
l, err := net.Listen("tcp", "127.0.0.1:8086")
if err != nil {
fmt.Println("服务器启动失败")
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err)
}
fmt.Printf("Received message %s -> %s \n", conn.RemoteAddr(), conn.LocalAddr())
ConnSlice[conn] = &Heartbeat{
endTime: time.Now().Add(time.Second * 5).Unix(), //初始化过期时间
}
go handelConn(conn)
}
}
func handelConn(c net.Conn) {
buffer := make([]byte, 1024)
for {
n, err := c.Read(buffer)
if ConnSlice[c].endTime > time.Now().Unix() {
ConnSlice[c].endTime = time.Now().Add(time.Second * 5).Unix() //更新心跳时间
} else {
fmt.Println("长时间未发消息断开连接")
return
}
if err != nil {
return
}
//如果是心跳检测,那就不要执行剩下的代码
if string(buffer[0:n]) == "1" {
c.Write([]byte("1"))
continue
}
for conn, heart := range ConnSlice {
if conn == c {
continue
}
//心跳检测 使用懒惰更新,需要发送数据的时候才检查规定时间内有没有数据到达
if heart.endTime < time.Now().Unix() {
delete(ConnSlice, conn) //从房间列表中删除连接,并且关闭
conn.Close()
fmt.Println("删除连接", conn.RemoteAddr())
fmt.Println("现在存有链接", ConnSlice)
continue
}
conn.Write(buffer[0:n])
}
}
}