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
bd8767f2
Commit
bd8767f2
authored
Oct 24, 2022
by
haoyanbin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
end_time
parent
c690c961
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
88 additions
and
77 deletions
+88
-77
client.go
pool/client.go
+20
-20
gopool.go
pool/gopool.go
+40
-45
publicApi.go
pool/publicApi.go
+8
-0
ws_server.go
ws_server.go
+20
-12
No files found.
pool/client.go
View file @
bd8767f2
...
...
@@ -209,14 +209,14 @@ loop:
break
loop
}
/
* /
/ToClientId与Channel不能同时存在!!!注意!!!!
if message.ToClientId!=
"" {
Send(message)
}
//ToClientId与Channel不能同时存在!!!注意!!!!
if message.Channel!=
"" {
Broadcast(message)
}*/
//ToClientId与Channel不能同时存在!!!注意!!!!
//if message.ToClientId !=
"" {
//
Send(message)
//
}
//ToClientId与Channel不能同时存在!!!注意!!!!
//if message.Channel !=
"" {
//
Broadcast(message)
//}
//收到消息触发回调
//c.onMessage(data)
...
...
@@ -266,19 +266,19 @@ Loop:
c
.
onError
(
errors
.
New
(
"连接ID:"
+
c
.
Id
+
"写消息进写入IO错误!连接中断"
+
err
.
Error
()))
goto
Loop1
}
/*// Add queued chat messages to the current websocket message.
n := len(c.sendCh)
if n > 0 {
for i := 0; i < n; i++ {
// Add queued chat messages to the current websocket message.
//n := len(c.sendCh)
//if n > 0 {
// for i := 0; i < n; i++ {
//
// _, err = w.Write(<-c.sendCh)
// if err != nil {
// c.onError(errors.New("连接ID:" + c.Id + "写上次连接未发送的消息消息进写入IO错误!连接中断" + err.Error()))
// return
// }
// }
//}
_, err = w.Write(<-c.sendCh)
if err != nil {
c.onError(errors.New("连接ID:" + c.Id + "写上次连接未发送的消息消息进写入IO错误!连接中断" + err.Error()))
return
}
}
}
*/
//关闭写入io对象
if
err
:=
w
.
Close
();
err
!=
nil
{
c
.
onError
(
errors
.
New
(
"连接ID:"
+
c
.
Id
+
"关闭写入IO对象出错,连接中断"
+
err
.
Error
()))
...
...
pool/gopool.go
View file @
bd8767f2
...
...
@@ -7,27 +7,25 @@ import (
// Goroutine Pool
type
Pool
struct
{
limit
int
// Max goroutine count limit.
count
int
// Current running goroutine count.
list
*
list
.
List
// Job list for asynchronous job adding purpose.
closed
bool
// Is pool closed or not.
limit
int
// Max goroutine count limit.
count
int
// Current running goroutine count.
list
*
list
.
List
// Job list for asynchronous job adding purpose.
closed
bool
// Is pool closed or not.
wJobsChan
chan
func
()
//写工作方法
rJobsChan
chan
func
()
//读工作方法
}
// New creates and returns a new goroutine pool object.
// The parameter <limit> is used to limit the max goroutine count,
// which is not limited in default.
func
New
(
limit
...
int
)
*
Pool
{
p
:=
&
Pool
{
limit
:
-
1
,
count
:
0
,
list
:
list
.
New
(),
closed
:
false
,
wJobsChan
:
make
(
chan
func
()),
rJobsChan
:
make
(
chan
func
()),
limit
:
-
1
,
count
:
0
,
list
:
list
.
New
(),
closed
:
false
,
wJobsChan
:
make
(
chan
func
()),
rJobsChan
:
make
(
chan
func
()),
}
if
len
(
limit
)
>
0
&&
limit
[
0
]
>
0
{
p
.
limit
=
limit
[
0
]
...
...
@@ -38,31 +36,28 @@ func New(limit ...int) *Pool {
return
p
}
/*
// Default goroutine pool.
var pool = New()
// Add pushes a new job to the pool using default goroutine pool.
// The job will be executed asynchronously.
func Add(f func()) error {
return pool.Add(f)
}
// Size returns current goroutine count of default goroutine pool.
func Size() int {
return pool.Size()
}
// Jobs returns current job count of default goroutine pool.
func Jobs() int {
return pool.Jobs()
}
*/
func
(
p
*
Pool
)
runWrite
(){
for
!
p
.
closed
{
//// Default goroutine pool.
//var pool = New()
//// Add pushes a new job to the pool using default goroutine pool.
//// The job will be executed asynchronously.
//func Add(f func()) error {
// return pool.Add(f)
//}
//
//// Size returns current goroutine count of default goroutine pool.
//func Size() int {
// return pool.Size()
//}
//
//// Jobs returns current job count of default goroutine pool.
//func Jobs() int {
// return pool.Jobs()
//}
func
(
p
*
Pool
)
runWrite
()
{
for
!
p
.
closed
{
select
{
case
f
,
ok
:=
<-
p
.
wJobsChan
:
case
f
,
ok
:=
<-
p
.
wJobsChan
:
if
!
ok
{
break
}
...
...
@@ -71,11 +66,11 @@ func (p *Pool) runWrite(){
}
}
func
(
p
*
Pool
)
runRead
(){
for
!
p
.
closed
{
func
(
p
*
Pool
)
runRead
()
{
for
!
p
.
closed
{
if
job
:=
p
.
list
.
Back
();
job
!=
nil
{
value
:=
p
.
list
.
Remove
(
job
)
p
.
rJobsChan
<-
value
.
(
func
())
p
.
rJobsChan
<-
value
.
(
func
())
}
else
{
return
}
...
...
@@ -85,17 +80,17 @@ func (p *Pool) runRead(){
// Add pushes a new job to the pool.
// The job will be executed asynchronously.
func
(
p
*
Pool
)
Add
(
f
func
())
error
{
for
p
.
closed
{
for
p
.
closed
{
return
errors
.
New
(
"pool closed"
)
}
p
.
wJobsChan
<-
f
p
.
wJobsChan
<-
f
var
n
int
n
=
p
.
count
n
=
p
.
count
if
p
.
limit
!=
-
1
&&
n
>=
p
.
limit
{
return
nil
}
p
.
count
=
n
+
1
p
.
count
=
n
+
1
p
.
fork
()
return
nil
}
...
...
@@ -125,7 +120,7 @@ func (p *Pool) fork() {
}()
for
!
p
.
closed
{
select
{
case
job
,
ok
:=
<-
p
.
rJobsChan
:
case
job
,
ok
:=
<-
p
.
rJobsChan
:
if
!
ok
{
break
}
...
...
@@ -142,7 +137,7 @@ func (p *Pool) IsClosed() bool {
// Close closes the goroutine pool, which makes all goroutines exit.
func
(
p
*
Pool
)
Close
()
{
p
.
closed
=
true
p
.
closed
=
true
close
(
p
.
wJobsChan
)
close
(
p
.
rJobsChan
)
}
pool/publicApi.go
View file @
bd8767f2
...
...
@@ -434,6 +434,14 @@ func PublishData(mqData *mq.SetMsgReq) {
return
}
func
SetEndTime
(){
mqData
:=
&
mq
.
SetMsgReq
{
ProcedureType
:
8
,
}
PublishData
(
mqData
)
return
}
// 五分钟内 用户未连接、则判定为用户离线、这时候把会话置为离线状态、如果中间顾客 连线了则return、 如果中间客服离线了、则return
//func (c *Client)SetOffline(user *UserInfo, customerId string) {
//
...
...
ws_server.go
View file @
bd8767f2
...
...
@@ -34,6 +34,14 @@ func main() {
fmt
.
Println
(
"wsPool.InitWsPool error-------------"
,
err
)
})
ticker
:=
time
.
NewTicker
(
time
.
Minute
)
go
func
()
{
for
range
ticker
.
C
{
pool
.
SetEndTime
()
}
}()
mux
:=
http
.
NewServeMux
()
mux
.
HandleFunc
(
"/"
,
serveHome
)
...
...
@@ -105,18 +113,18 @@ func ws(w http.ResponseWriter, r *http.Request) {
// 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())
}*/
/
/
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())
//}
})
...
...
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