Skip to content

Commit ea27836

Browse files
author
hero
committed
添加推送服务
1 parent e800ec2 commit ea27836

File tree

1 file changed

+51
-11
lines changed

1 file changed

+51
-11
lines changed

go_push/gateway/push_job.go

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,71 @@
11
package gateway
22

3+
import "fmt"
4+
35
type PushJob struct {
4-
Type int
5-
roomId string
6-
info []byte
6+
Type int
7+
PushType int
8+
roomId int
9+
info string
710
}
811

912
type PushTask struct {
1013
JobChan []chan *PushJob
1114
DistributionTask chan *PushJob
1215
}
1316

14-
func NewPushTask() *PushTask {
17+
func NewPushTask(roomLen, workNum, taskNum int) *PushTask {
1518
var p *PushTask
16-
p.DistributionTask = make(chan *PushJob, 10)
17-
p.JobChan = make([]chan *PushJob, len(roomTitle)) //一个房间对应一个job
18-
for i, _ := range roomTitle {
19-
go p.distributionTask(i) //分发任务
19+
p.DistributionTask = make(chan *PushJob, taskNum)
20+
p.JobChan = make([]chan *PushJob, roomLen)
21+
for i := 0; i < roomLen; i++ {
22+
//可以一个房间开多个pushWork(i)
23+
go p.pushWork(i) //分发任务
24+
}
25+
for i := 0; i < workNum; i++ {
26+
go p.distributionTask()
2027
}
2128
return p
2229
}
2330

24-
func (p *PushTask) distributionTask(roomId int) {
31+
func (p *PushTask) Push(job *PushJob) {
32+
p.DistributionTask <- job
33+
}
2534

35+
func (p *PushTask) distributionTask() {
36+
var (
37+
pushJob *PushJob
38+
)
39+
for {
40+
select {
41+
case pushJob = <-p.DistributionTask:
42+
// 分发
43+
if pushJob.Type == 1 {
44+
GetRoomManage().PushAll(&WSMessage{
45+
Type: pushJob.PushType,
46+
Data: pushJob.info,
47+
})
48+
} else if pushJob.Type == 2 {
49+
p.JobChan[pushJob.roomId] <- pushJob
50+
}
51+
}
52+
}
2653
}
2754

2855
func (p *PushTask) pushWork(roomId int) {
29-
var (
30-
)
56+
var (
57+
err error
58+
job *PushJob
59+
)
60+
for {
61+
select {
62+
case job = <-p.JobChan[roomId]:
63+
if err = GetRoomManage().PushRoom(roomId, &WSMessage{
64+
Type: job.PushType,
65+
Data: job.info,
66+
}); err != nil {
67+
fmt.Println(err)
68+
}
69+
}
70+
}
3171
}

0 commit comments

Comments
 (0)