Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
im-pool
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
haoyanbin
im-pool
Commits
be273959
Commit
be273959
authored
Jan 05, 2023
by
haoyanbin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Client.user
parent
81a75d9a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
116 additions
and
89 deletions
+116
-89
client.go
pool/client.go
+4
-2
hub.go
pool/hub.go
+35
-21
publicApi.go
pool/publicApi.go
+66
-56
ws_server.go
ws_server.go
+11
-10
No files found.
pool/client.go
View file @
be273959
...
@@ -43,10 +43,11 @@ var upgrader = websocket.Upgrader{
...
@@ -43,10 +43,11 @@ var upgrader = websocket.Upgrader{
//连接参数结构体
//连接参数结构体
type
Config
struct
{
type
Config
struct
{
Id
string
//标识连接的名称
Id
string
//标识连接的名称
/用户组标识
Type
string
//连接类型或path
Type
string
//连接类型或path
Channel
[]
string
//连接注册频道类型方便广播等操作。做为一个数组存储。因为一个连接可以属多个频道
Channel
[]
string
//连接注册频道类型方便广播等操作。做为一个数组存储。因为一个连接可以属多个频道
Goroutine
int
//每个连接开启的go程数里 默认为10
Goroutine
int
//每个连接开启的go程数里 默认为10
User
string
//用户唯一标识
}
}
type
RuntimeInfo
struct
{
type
RuntimeInfo
struct
{
...
@@ -85,6 +86,7 @@ type Client struct {
...
@@ -85,6 +86,7 @@ type Client struct {
lastReceiveTime
time
.
Time
//最后一次接收到数据的时间
lastReceiveTime
time
.
Time
//最后一次接收到数据的时间
lastSendTime
time
.
Time
//最后一次发送数据的时间
lastSendTime
time
.
Time
//最后一次发送数据的时间
Id
string
//标识连接的名称
Id
string
//标识连接的名称
User
string
//标识连接的名称
mux
*
sync
.
Mutex
mux
*
sync
.
Mutex
IsClose
chan
bool
//连接的状态。true为关闭
IsClose
chan
bool
//连接的状态。true为关闭
channel
[]
string
//连接注册频道类型方便广播等操作。做为一个数组存储。因为一个连接可以属多个频道
channel
[]
string
//连接注册频道类型方便广播等操作。做为一个数组存储。因为一个连接可以属多个频道
...
@@ -141,7 +143,7 @@ func (c *Client) readPump() {
...
@@ -141,7 +143,7 @@ func (c *Client) readPump() {
close
(
c
.
sendPing
)
close
(
c
.
sendPing
)
c
.
grpool
.
Close
()
c
.
grpool
.
Close
()
c
.
hub
.
RemoveClient
(
c
)
c
.
hub
.
RemoveClient
(
c
)
DelClient
(
c
.
Id
)
DelClient
(
c
.
Id
,
c
.
User
)
dump
()
dump
()
}()
}()
Loop
:
Loop
:
...
...
pool/hub.go
View file @
be273959
...
@@ -27,8 +27,8 @@ var (
...
@@ -27,8 +27,8 @@ var (
// clients.
// clients.
type
hub
struct
{
type
hub
struct
{
// Registered clients.
// Registered clients.
clients
map
[
string
]
*
Client
//新的处理方式有没有线程效率会更高,所以SafeMap的锁处理都去掉了
clients
map
[
string
]
map
[
string
]
*
Client
//新的处理方式有没有线程效率会更高,所以SafeMap的锁处理都去掉了
oldClients
map
[
string
]
*
Client
//缓存断开的连接消息队列
oldClients
map
[
string
]
map
[
string
]
*
Client
//缓存断开的连接消息队列
// Inbound messages from the clients.
// Inbound messages from the clients.
//可以用于广播所有连接对象
//可以用于广播所有连接对象
...
@@ -57,8 +57,8 @@ func newHub() *hub {
...
@@ -57,8 +57,8 @@ func newHub() *hub {
//conversation: make([]*Conversation, MaxConversationNum),
//conversation: make([]*Conversation, MaxConversationNum),
register
:
make
(
chan
*
Client
,
MaxClientChannelLen
),
register
:
make
(
chan
*
Client
,
MaxClientChannelLen
),
unregister
:
make
(
chan
string
,
MaxClientChannelLen
),
unregister
:
make
(
chan
string
,
MaxClientChannelLen
),
clients
:
make
(
map
[
string
]
*
Client
),
clients
:
make
(
map
[
string
]
map
[
string
]
*
Client
),
oldClients
:
make
(
map
[
string
]
*
Client
),
oldClients
:
make
(
map
[
string
]
map
[
string
]
*
Client
),
broadcastQueue
:
make
(
chan
*
SendMsg
,
MaxBroadcastQueueLen
),
broadcastQueue
:
make
(
chan
*
SendMsg
,
MaxBroadcastQueueLen
),
chanBroadcastQueue
:
make
(
chan
*
SendMsg
,
MaxChanBroadcastQueueLen
),
chanBroadcastQueue
:
make
(
chan
*
SendMsg
,
MaxChanBroadcastQueueLen
),
}
}
...
@@ -72,28 +72,38 @@ loop:
...
@@ -72,28 +72,38 @@ loop:
if
!
ok
{
if
!
ok
{
break
loop
break
loop
}
}
c
,
_
:=
h
.
clients
[
id
]
userInfo
,
_
:=
GetClientInfo
(
id
)
c
,
_
:=
h
.
clients
[
userInfo
.
ClientId
][
userInfo
.
User
]
if
c
!=
nil
{
if
c
!=
nil
{
delete
(
h
.
clients
,
id
)
delete
(
h
.
clients
[
userInfo
.
ClientId
],
userInfo
.
User
)
}
}
fmt
.
Println
(
"取消注册ws连接对象:"
,
id
,
"连接总数:"
,
len
(
h
.
clients
))
fmt
.
Println
(
"取消注册ws连接对象:"
,
id
,
"连接总数:"
,
len
(
h
.
clients
))
if
len
(
h
.
clients
[
userInfo
.
ClientId
])
==
0
{
delete
(
h
.
clients
,
userInfo
.
ClientId
)
}
case
client
,
ok
:=
<-
h
.
register
:
case
client
,
ok
:=
<-
h
.
register
:
if
!
ok
{
if
!
ok
{
break
loop
break
loop
}
}
h
.
clients
[
client
.
Id
]
=
client
if
len
(
h
.
clients
[
client
.
Id
])
>
0
{
fmt
.
Println
(
"注册ws连接对象:"
,
client
.
Id
,
"连接总数:"
,
len
(
h
.
clients
))
h
.
clients
[
client
.
Id
][
client
.
User
]
=
client
}
else
{
h
.
clients
[
client
.
Id
]
=
map
[
string
]
*
Client
{
client
.
User
:
client
}
}
fmt
.
Println
(
"注册ws连接对象:"
,
client
.
User
,
"连接总数:"
,
len
(
h
.
clients
))
case
broadcastMsg
,
ok
:=
<-
h
.
broadcastQueue
:
case
broadcastMsg
,
ok
:=
<-
h
.
broadcastQueue
:
if
!
ok
{
if
!
ok
{
break
loop
break
loop
}
}
for
k
,
v
:=
range
h
.
clients
{
for
_
,
v
:=
range
h
.
clients
{
if
v
!=
nil
{
if
v
!=
nil
{
client
:=
v
for
k
,
vv
:=
range
v
{
broadcastMsg
.
ToClientId
=
k
client
:=
vv
client
.
send
(
broadcastMsg
)
broadcastMsg
.
ToClientId
=
k
client
.
send
(
broadcastMsg
)
}
}
}
}
}
...
@@ -103,13 +113,15 @@ loop:
...
@@ -103,13 +113,15 @@ loop:
}
}
//广播指定频道的消息处理
//广播指定频道的消息处理
//h.clients.Iterator(func(id string, v interface{}) bool {
//h.clients.Iterator(func(id string, v interface{}) bool {
for
k
,
v
:=
range
h
.
clients
{
for
_
,
v
:=
range
h
.
clients
{
if
v
!=
nil
{
if
v
!=
nil
{
client
:=
v
for
k
,
vv
:=
range
v
{
for
_
,
ch
:=
range
chanBroadcastMsg
.
Channel
{
client
:=
vv
if
searchStrArray
(
client
.
channel
,
ch
)
{
for
_
,
ch
:=
range
chanBroadcastMsg
.
Channel
{
chanBroadcastMsg
.
ToClientId
=
k
if
searchStrArray
(
client
.
channel
,
ch
)
{
client
.
send
(
chanBroadcastMsg
)
chanBroadcastMsg
.
ToClientId
=
k
client
.
send
(
chanBroadcastMsg
)
}
}
}
}
}
}
}
...
@@ -117,7 +129,6 @@ loop:
...
@@ -117,7 +129,6 @@ loop:
// return true
// return true
//})
//})
}
}
}
}
}
}
func
(
h
*
hub
)
ticker
()
{
func
(
h
*
hub
)
ticker
()
{
...
@@ -156,7 +167,10 @@ func (h *hub) AddClient(client *Client) error {
...
@@ -156,7 +167,10 @@ func (h *hub) AddClient(client *Client) error {
func
(
h
*
hub
)
clearOldClient
(
client
*
Client
)
{
func
(
h
*
hub
)
clearOldClient
(
client
*
Client
)
{
close
(
client
.
recvCh
)
close
(
client
.
recvCh
)
close
(
client
.
sendCh
)
close
(
client
.
sendCh
)
delete
(
h
.
oldClients
,
client
.
Id
)
delete
(
h
.
oldClients
[
client
.
Id
],
client
.
User
)
if
len
(
h
.
oldClients
[
client
.
Id
])
==
0
{
delete
(
h
.
oldClients
,
client
.
Id
)
}
fmt
.
Println
(
"己断开的ws连接缓存对象:"
,
client
.
Id
,
"旧连接总数:"
,
len
(
h
.
oldClients
))
fmt
.
Println
(
"己断开的ws连接缓存对象:"
,
client
.
Id
,
"旧连接总数:"
,
len
(
h
.
oldClients
))
}
}
...
@@ -167,7 +181,7 @@ func (h *hub) RemoveClient(client *Client) error {
...
@@ -167,7 +181,7 @@ func (h *hub) RemoveClient(client *Client) error {
timeout
:=
time
.
NewTimer
(
time
.
Second
*
1
)
timeout
:=
time
.
NewTimer
(
time
.
Second
*
1
)
defer
timeout
.
Stop
()
defer
timeout
.
Stop
()
select
{
select
{
case
h
.
unregister
<-
client
.
Id
:
case
h
.
unregister
<-
client
.
User
:
return
nil
return
nil
case
<-
timeout
.
C
:
case
<-
timeout
.
C
:
return
errors
.
New
(
" RemoveClient unregister消息管道blocked,写入消息超时"
)
return
errors
.
New
(
" RemoveClient unregister消息管道blocked,写入消息超时"
)
...
...
pool/publicApi.go
View file @
be273959
...
@@ -38,14 +38,18 @@ func NewClient(conf *Config) *Client {
...
@@ -38,14 +38,18 @@ func NewClient(conf *Config) *Client {
conf
.
Goroutine
=
200
conf
.
Goroutine
=
200
}
}
var
client
*
Client
var
client
*
Client
oldclient
:=
wsSever
.
hub
.
oldClients
[
conf
.
Id
]
oldclient
:=
wsSever
.
hub
.
oldClients
[
conf
.
Id
]
[
conf
.
User
]
if
oldclient
!=
nil
{
if
oldclient
!=
nil
{
delete
(
wsSever
.
hub
.
oldClients
,
conf
.
Id
)
delete
(
wsSever
.
hub
.
oldClients
[
conf
.
Id
],
conf
.
User
)
if
len
(
wsSever
.
hub
.
oldClients
[
conf
.
Id
])
==
0
{
delete
(
wsSever
.
hub
.
oldClients
,
conf
.
Id
)
}
c
:=
oldclient
c
:=
oldclient
client
=
c
client
=
c
}
else
{
}
else
{
client
=
&
Client
{
client
=
&
Client
{
Id
:
conf
.
Id
,
Id
:
conf
.
Id
,
User
:
conf
.
User
,
types
:
conf
.
Type
,
types
:
conf
.
Type
,
hub
:
wsSever
.
hub
,
hub
:
wsSever
.
hub
,
sendCh
:
make
(
chan
*
SendMsg
,
MaxRecvChLen
),
sendCh
:
make
(
chan
*
SendMsg
,
MaxRecvChLen
),
...
@@ -65,7 +69,7 @@ func NewClient(conf *Config) *Client {
...
@@ -65,7 +69,7 @@ func NewClient(conf *Config) *Client {
client
.
OnPing
(
nil
)
client
.
OnPing
(
nil
)
client
.
OnPong
(
nil
)
client
.
OnPong
(
nil
)
wsSever
.
hub
.
AddClient
(
client
)
wsSever
.
hub
.
AddClient
(
client
)
AppendClient
(
client
.
Id
,
string
(
SerializeJson
(
conf
)))
AppendClient
(
client
.
Id
,
client
.
User
,
string
(
SerializeJson
(
conf
)))
return
client
return
client
}
}
...
@@ -223,7 +227,7 @@ func (c *Client) Send(msg *SendMsg) error {
...
@@ -223,7 +227,7 @@ func (c *Client) Send(msg *SendMsg) error {
c
.
close
()
c
.
close
()
return
errors
.
New
(
"连接ID:"
+
c
.
Id
+
"ws连接己经断开,无法发送消息"
)
return
errors
.
New
(
"连接ID:"
+
c
.
Id
+
"ws连接己经断开,无法发送消息"
)
default
:
default
:
msg
.
ToClientId
=
c
.
Id
msg
.
ToClientId
=
c
.
User
fmt
.
Println
(
msg
)
fmt
.
Println
(
msg
)
c
.
send
(
msg
)
c
.
send
(
msg
)
}
}
...
@@ -245,19 +249,23 @@ func Send(msg *SendMsg) error {
...
@@ -245,19 +249,23 @@ func Send(msg *SendMsg) error {
if
msg
.
ToClientId
==
""
{
if
msg
.
ToClientId
==
""
{
return
errors
.
New
(
"发送消息的消息体中未指定ToClient目标!"
)
return
errors
.
New
(
"发送消息的消息体中未指定ToClient目标!"
)
}
}
c
:=
wsSever
.
hub
.
clients
[
msg
.
ToClientId
]
if
c
!=
nil
{
userInfo
,
_
:=
GetClientInfo
(
msg
.
ToClientId
)
client
:=
c
cList
:=
wsSever
.
hub
.
clients
[
userInfo
.
ClientId
]
if
client
.
Id
==
msg
.
ToClientId
{
for
_
,
c
:=
range
cList
{
msg
.
ToClientId
=
client
.
Id
if
c
!=
nil
{
err
:=
client
.
Send
(
msg
)
//client := c
//if client.Id == msg.ToClientId {
msg
.
ToClientId
=
c
.
User
err
:=
c
.
Send
(
msg
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
New
(
"发送消息出错:"
+
err
.
Error
()
+
",连接对象id="
+
c
lient
.
Id
+
"。"
)
return
errors
.
New
(
"发送消息出错:"
+
err
.
Error
()
+
",连接对象id="
+
c
.
User
+
"。"
)
}
}
//}
}
else
{
fmt
.
Println
(
1
)
//fmt.Println(wsSever.hub.clients)
}
}
}
else
{
fmt
.
Println
(
1
)
//fmt.Println(wsSever.hub.clients)
}
}
return
nil
return
nil
...
@@ -296,8 +304,8 @@ func BroadcastAll(msg *SendMsg) error {
...
@@ -296,8 +304,8 @@ func BroadcastAll(msg *SendMsg) error {
}
}
// 根据来源和类型获取客户端列表
// 根据来源和类型获取客户端列表
func
GetList
(
source
,
sourceId
,
customerType
string
)
map
[
string
]
*
Client
{
func
GetList
(
source
,
sourceId
,
customerType
string
)
map
[
string
]
map
[
string
]
*
Client
{
clientData
:=
make
(
map
[
string
]
*
Client
,
0
)
clientData
:=
make
(
map
[
string
]
map
[
string
]
*
Client
,
0
)
for
k
,
v
:=
range
wsSever
.
hub
.
clients
{
for
k
,
v
:=
range
wsSever
.
hub
.
clients
{
userInfo
:=
strings
.
Split
(
k
,
"_"
)
userInfo
:=
strings
.
Split
(
k
,
"_"
)
if
(
userInfo
[
0
]
==
source
&&
userInfo
[
1
]
==
sourceId
)
||
(
source
==
""
)
{
if
(
userInfo
[
0
]
==
source
&&
userInfo
[
1
]
==
sourceId
)
||
(
source
==
""
)
{
...
@@ -319,13 +327,14 @@ type UserInfo struct {
...
@@ -319,13 +327,14 @@ type UserInfo struct {
CustomerType
string
`json:"customerType"`
CustomerType
string
`json:"customerType"`
CustomerId
string
`json:"customerId"`
CustomerId
string
`json:"customerId"`
ClientId
string
`json:"clientId"`
ClientId
string
`json:"clientId"`
User
string
`json:"User"`
ToChannel
[]
string
`json:"toChannel"`
ToChannel
[]
string
`json:"toChannel"`
}
}
//根据token获取用户来源信息
//根据token获取用户来源信息
func
GetClientInfo
ByToken
(
token
string
)
(
*
UserInfo
,
error
)
{
func
GetClientInfo
(
id
string
)
(
*
UserInfo
,
error
)
{
tokenData
:=
strings
.
Split
(
token
,
"_"
)
tokenData
:=
strings
.
Split
(
id
,
"_"
)
fmt
.
Println
(
tokenData
)
fmt
.
Println
(
tokenData
)
if
len
(
tokenData
)
<
4
{
if
len
(
tokenData
)
<
4
{
...
@@ -349,7 +358,8 @@ func GetClientInfoByToken(token string) (*UserInfo, error) {
...
@@ -349,7 +358,8 @@ func GetClientInfoByToken(token string) (*UserInfo, error) {
userData
.
CustomerType
=
tokenData
[
2
]
userData
.
CustomerType
=
tokenData
[
2
]
userData
.
CustomerId
=
tokenData
[
3
]
userData
.
CustomerId
=
tokenData
[
3
]
userData
.
ClientId
=
userData
.
Source
+
"_"
+
userData
.
SourceId
+
"_"
+
userData
.
CustomerType
+
"_"
+
userData
.
CustomerId
userData
.
ClientId
=
userData
.
SourceId
+
"_"
+
userData
.
CustomerType
+
"_"
+
userData
.
CustomerId
userData
.
User
=
userData
.
Source
+
"_"
+
userData
.
SourceId
+
"_"
+
userData
.
CustomerType
+
"_"
+
userData
.
CustomerId
return
userData
,
nil
return
userData
,
nil
}
}
...
@@ -361,7 +371,7 @@ func SaveMsg(msg *SendMsg) {
...
@@ -361,7 +371,7 @@ func SaveMsg(msg *SendMsg) {
}
}
msg
.
SendTime
=
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
)
msg
.
SendTime
=
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
)
user
:=
GetClientInfoById
(
msg
.
FromClientId
)
user
,
_
:=
GetClientInfo
(
msg
.
FromClientId
)
mqData
:=
&
SetMsgReq
{}
mqData
:=
&
SetMsgReq
{}
mqData
.
ProcedureType
=
msg
.
ProcedureType
mqData
.
ProcedureType
=
msg
.
ProcedureType
...
@@ -477,49 +487,49 @@ func DelConversation(conversationId int) {
...
@@ -477,49 +487,49 @@ func DelConversation(conversationId int) {
return
return
}
}
func
AppendClient
(
clientId
,
data
string
)
{
func
AppendClient
(
clientId
,
clientUser
,
data
string
)
{
Redis
.
HSet
(
cliKey
,
clientId
,
data
)
Redis
.
HSet
(
cliKey
+
":"
+
clientId
,
clientUser
,
data
)
return
return
}
}
func
DelClient
(
clientId
string
)
{
func
DelClient
(
clientId
,
clientUser
string
)
{
Redis
.
HDel
(
cliKey
,
clientId
)
Redis
.
HDel
(
cliKey
+
":"
+
clientId
,
clientUser
)
return
return
}
}
func
SetEnd
()
{
func
SetEnd
()
{
conversationList
:=
GetAllList
(
convKey
)
//
conversationList := GetAllList(convKey)
for
_
,
vClients
:=
range
conversationList
{
//
for _, vClients := range conversationList {
conversationData
:=
&
Conversation
{}
//
conversationData := &Conversation{}
UnserislizeJson
([]
byte
(
vClients
),
conversationData
)
//
UnserislizeJson([]byte(vClients), conversationData)
if
conversationData
.
StartReceiveDate
<
time
.
Now
()
.
Add
(
-
endDate
)
.
Format
(
"2006-01-02 15:04:05"
)
{
//
if conversationData.StartReceiveDate < time.Now().Add(-endDate).Format("2006-01-02 15:04:05") {
mqData
:=
&
SendMsg
{
//
mqData := &SendMsg{
ProcedureType
:
8
,
//
ProcedureType: 8,
ConversationId
:
conversationData
.
ConversationId
,
//
ConversationId: conversationData.ConversationId,
ToClientId
:
conversationData
.
Promoter
,
//
ToClientId: conversationData.Promoter,
FromClientId
:
conversationData
.
Participant
,
//
FromClientId: conversationData.Participant,
SendTime
:
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
),
//
SendTime: time.Now().Format("2006-01-02 15:04:05"),
}
//
}
//发送结束会话给客户端
//
//发送结束会话给客户端
_
,
isSet
:=
wsSever
.
hub
.
clients
[
conversationData
.
Promoter
]
//
_, isSet := wsSever.hub.clients[conversationData.Promoter]
if
isSet
==
true
{
//
if isSet == true {
wsSever
.
hub
.
clients
[
conversationData
.
Promoter
]
.
readMessage
(
mqData
)
//
wsSever.hub.clients[conversationData.Promoter].readMessage(mqData)
}
//
}
//
toMqData
:=
&
SendMsg
{
//
toMqData := &SendMsg{
ProcedureType
:
8
,
//
ProcedureType: 8,
ConversationId
:
conversationData
.
ConversationId
,
//
ConversationId: conversationData.ConversationId,
ToClientId
:
conversationData
.
Participant
,
//
ToClientId: conversationData.Participant,
FromClientId
:
conversationData
.
Promoter
,
//
FromClientId: conversationData.Promoter,
SendTime
:
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
),
//
SendTime: time.Now().Format("2006-01-02 15:04:05"),
}
//
}
_
,
isSet
=
wsSever
.
hub
.
clients
[
conversationData
.
Participant
]
//
_, isSet = wsSever.hub.clients[conversationData.Participant]
if
isSet
==
true
{
//
if isSet == true {
wsSever
.
hub
.
clients
[
conversationData
.
Participant
]
.
readMessage
(
toMqData
)
//
wsSever.hub.clients[conversationData.Participant].readMessage(toMqData)
}
//
}
SaveMsg
(
toMqData
)
//
SaveMsg(toMqData)
}
//
}
}
//
}
return
return
}
}
...
...
ws_server.go
View file @
be273959
...
@@ -75,7 +75,7 @@ func ws(w http.ResponseWriter, r *http.Request) {
...
@@ -75,7 +75,7 @@ func ws(w http.ResponseWriter, r *http.Request) {
head
:=
http
.
Header
{}
head
:=
http
.
Header
{}
head
.
Add
(
"Sec-Websocket-Protocol"
,
headData
)
head
.
Add
(
"Sec-Websocket-Protocol"
,
headData
)
userInfo
,
err
:=
pool
.
GetClientInfo
ByToken
(
headData
)
userInfo
,
err
:=
pool
.
GetClientInfo
(
headData
)
if
err
!=
nil
{
if
err
!=
nil
{
fmt
.
Println
(
"用户信息报错:"
,
err
)
fmt
.
Println
(
"用户信息报错:"
,
err
)
...
@@ -84,17 +84,18 @@ func ws(w http.ResponseWriter, r *http.Request) {
...
@@ -84,17 +84,18 @@ func ws(w http.ResponseWriter, r *http.Request) {
//实例化连接对象
//实例化连接对象
client
:=
pool
.
NewClient
(
&
pool
.
Config
{
client
:=
pool
.
NewClient
(
&
pool
.
Config
{
Id
:
userInfo
.
ClientId
,
//连接标识
Id
:
userInfo
.
ClientId
,
//连接标识
User
:
userInfo
.
User
,
Type
:
"ws"
,
//连接类型
Type
:
"ws"
,
//连接类型
Channel
:
userInfo
.
ToChannel
,
//指定频道
Channel
:
userInfo
.
ToChannel
,
//指定频道
Goroutine
:
100
,
Goroutine
:
100
,
})
})
fmt
.
Println
(
client
.
Id
,
"实例化连接对象完成"
)
fmt
.
Println
(
client
.
User
,
"实例化连接对象完成"
)
//连接成功回调
//连接成功回调
client
.
OnOpen
(
func
()
{
client
.
OnOpen
(
func
()
{
fmt
.
Println
(
"连接开启回调:"
,
client
.
Id
)
fmt
.
Println
(
"连接开启回调:"
,
client
.
User
)
})
})
//接收消息
//接收消息
...
@@ -134,26 +135,26 @@ func ws(w http.ResponseWriter, r *http.Request) {
...
@@ -134,26 +135,26 @@ func ws(w http.ResponseWriter, r *http.Request) {
//连接断开回调
//连接断开回调
client
.
OnClose
(
func
()
{
client
.
OnClose
(
func
()
{
user
:=
pool
.
GetClientInfoById
(
client
.
Id
)
user
,
_
:=
pool
.
GetClientInfo
(
client
.
User
)
closeMsg
:=
&
pool
.
SetMsgReq
{}
closeMsg
:=
&
pool
.
SetMsgReq
{}
closeMsg
.
ProcedureType
=
5
closeMsg
.
ProcedureType
=
5
closeMsg
.
EndTime
=
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
)
closeMsg
.
EndTime
=
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
)
if
user
.
CustomerType
==
"1"
{
if
user
.
CustomerType
==
"1"
{
closeMsg
.
Promoter
=
client
.
Id
closeMsg
.
Promoter
=
client
.
User
pool
.
PublishData
(
closeMsg
)
pool
.
PublishData
(
closeMsg
)
fmt
.
Println
(
"用户关闭连接"
,
client
.
Id
)
fmt
.
Println
(
"用户关闭连接"
,
client
.
User
)
}
}
if
user
.
CustomerType
==
"2"
{
if
user
.
CustomerType
==
"2"
{
closeMsg
.
Participant
=
client
.
Id
closeMsg
.
Participant
=
client
.
User
pool
.
PublishData
(
closeMsg
)
pool
.
PublishData
(
closeMsg
)
fmt
.
Println
(
"专家关闭连接"
,
client
.
Id
)
fmt
.
Println
(
"专家关闭连接"
,
client
.
User
)
}
}
pool
.
DelClient
(
client
.
Id
)
pool
.
DelClient
(
client
.
Id
,
client
.
User
)
fmt
.
Printf
(
"连接己经关闭%s"
,
client
.
Id
)
fmt
.
Printf
(
"连接己经关闭%s"
,
client
.
Id
)
})
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment