-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
126 lines (103 loc) · 2.14 KB
/
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"fmt"
"io"
"net"
"sync"
"time"
)
type Server struct {
Ip string
Port int
// 在线用户列表
OnlineMap map[string]*User
MapLock sync.RWMutex
// 消息广播channel
Message chan string
}
// NewServer 创建server实例
func NewServer(ip string, port int) *Server {
return &Server{
Ip: ip,
Port: port,
OnlineMap: make(map[string]*User),
Message: make(chan string),
}
}
// 监听message 广播消息channel 的 goroutine, 一旦有消息就广播给所有在线user
func (serv *Server) ListenMessage() {
for {
msg := <-serv.Message
serv.MapLock.Lock()
// 消息发送给全部在线user
for _, cli := range serv.OnlineMap {
cli.C <- msg
}
serv.MapLock.Unlock()
}
}
// BroadCast 广播消息
func (serv *Server) BroadCast(user *User, msg string) {
sendMsg := "[" + user.Addr + "]" + user.Name + ":" + msg
serv.Message <- sendMsg
}
func (serv *Server) Handle(conn net.Conn) {
user := NewUser(conn, serv)
// 用户上线后,将用户加入onlineMap中
user.Online()
// 接收客户端发送的消息
isActive := make(chan bool)
go func() {
buf := make([]byte, 4096)
for {
n, err := conn.Read(buf)
if n == 0 {
user.Offline()
return
}
if err != nil && err != io.EOF {
fmt.Println("Conn read error", err)
return
}
// 提取用户消息 去除(\n)
msg := string(buf[:n-1])
user.DoMessage(msg)
isActive <- true
}
}()
for {
select {
case <-isActive:
// 当前用户是活跃的,重置定时器
// 不做任何事情,为了激活select 更新下面的定时器
case <-time.After(10 * time.Hour):
user.SendMsg("Time out")
close(user.C)
err := conn.Close()
if err != nil {
return
}
return
}
}
}
func (serv *Server) Start() {
// socket listen
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", serv.Ip, serv.Port))
if err != nil {
fmt.Println("net.Listen:", err)
return
}
// close listen
defer listener.Close()
go serv.ListenMessage()
// accept
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Listener accept err:", err)
continue
}
go serv.Handle(conn)
}
}