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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"flag"
"fmt"
"net/http"
"net/http/pprof"
"pool/dao/mq"
"pool/dao/redis"
"pool/pool"
"runtime"
"time"
)
func serveHome(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL)
if r.URL.Path != "/" {
http.Error(w, "Not found", http.StatusNotFound)
return
}
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
http.ServeFile(w, r, "home.html")
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
pool.RabbitMQ = mq.NewRabbitMQSimple("im")
fmt.Println("rabbitMq start success")
pool.Redis = redis.Init()
fmt.Println("redis start success")
//初骀化连接池
pool.InitWsPool(func(err interface{}) {
//接收连接池中的运行时错误信息
fmt.Println("wsPool.InitWsPool error-------------", err)
})
//自动结束会话
ticker := time.NewTicker(time.Second * 10)
go func() {
for range ticker.C {
pool.SetEnd()
}
}()
mux := http.NewServeMux()
mux.HandleFunc("/", serveHome)
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
mux.HandleFunc("/ws", ws)
mux.HandleFunc("/ws-list", GetClientList)
err := http.ListenAndServe(":11001", mux)
if err != nil {
fmt.Printf("ListenAndServe: %s", err.Error())
}
}
func ws(w http.ResponseWriter, r *http.Request) {
headData := r.Header.Get("Sec-Websocket-Protocol")
head := http.Header{}
head.Add("Sec-Websocket-Protocol", headData)
userInfo, err := pool.GetClientInfoByToken(headData)
if err != nil {
fmt.Println("用户信息报错:", err)
return
}
//实例化连接对象
client := pool.NewClient(&pool.Config{
Id: userInfo.ClientId, //连接标识
Type: "ws", //连接类型
Channel: userInfo.ToChannel, //指定频道
Goroutine: 100,
})
fmt.Println(client.Id, "实例化连接对象完成")
//连接成功回调
client.OnOpen(func() {
fmt.Println("连接开启回调:", client.Id)
})
//接收消息
client.OnMessage(func(msg *pool.SendMsg) {
if msg.Status == 3 {
fmt.Println("OnMessage:收到出错消息=》", client.Id, msg.Desc)
return
}
//fmt.Println(msg.Msg)
if msg.ToClientId != "" {
//发送消息给指定的ToClientID连接
err := pool.Send(msg)
if err != nil {
fmt.Println("pool.Send(msg):", err.Error())
}
//发送消息给当前连接对象
//err = client.Send(msg)
//if err != nil {
// fmt.Println("client.Send(msg):", err.Error())
//}
}
//if len(msg.Channel)>0{
// //按频道广播,可指定多个频道[]string
// err:=pool.Broadcast(msg) //或者 wsPool.Broadcast(msg)
// if err!=nil {
// fmt.Println("pool.Broadcast(msg)", err.Error())
// }
//}
////或都全局广播,所有连接都进行发送
//err:=pool.BroadcastAll(msg)
//if err!=nil {
// fmt.Println("pool.BroadcastAll(msg)", err.Error())
//}
})
//连接断开回调
client.OnClose(func() {
user := pool.GetClientInfoById(client.Id)
closeMsg := &pool.SetMsgReq{}
closeMsg.ProcedureType = 5
closeMsg.EndTime = time.Now().Format("2006-01-02 15:04:05")
if user.CustomerType == "1" {
closeMsg.Promoter = client.Id
pool.PublishData(closeMsg)
fmt.Println("用户关闭连接", client.Id)
}
if user.CustomerType == "2" {
closeMsg.Participant = client.Id
pool.PublishData(closeMsg)
fmt.Println("专家关闭连接", client.Id)
}
pool.DelClient(client.Id)
fmt.Printf("连接己经关闭%s", client.Id)
})
client.OnError(func(err error) {
fmt.Printf("连接%s错误信息:%s", client.Id, err.Error())
})
//开启连接
client.OpenClient(w, r, head)
fmt.Println(client.Id, "开启连接")
r.Close = true
return
}
func GetClientList(w http.ResponseWriter, r *http.Request) {
data := r.URL.Query()
list := pool.GetList(data["source"][0], data["sourceId"][0], data["customerType"][0])
reply := make([]string, 0)
for k := range list {
reply = append(reply, k)
}
resp(w, reply)
}
func resp(w http.ResponseWriter, data interface{}) {
w.Header().Set("content-type", "text/json")
w.WriteHeader(200)
w.Write(pool.SerializeJson(data))
}
type GetClientListReq struct {
Source string `json:"source" form:"source"`
Promoter string `json:"promoter" form:"promoter"`
}