forked from LockGit/gochat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.go
64 lines (57 loc) · 1.38 KB
/
push.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
/**
* Created by lock
* Date: 2019-08-13
* Time: 10:50
*/
package task
import (
"encoding/json"
"github.com/sirupsen/logrus"
"gochat/config"
"gochat/proto"
"math/rand"
)
type PushParams struct {
ServerId int
UserId int
Msg []byte
RoomId int
}
var pushChannel []chan *PushParams
func init() {
pushChannel = make([]chan *PushParams, config.Conf.Task.TaskBase.PushChan)
}
func (task *Task) GoPush() {
for i := 0; i < len(pushChannel); i++ {
pushChannel[i] = make(chan *PushParams, config.Conf.Task.TaskBase.PushChanSize)
go task.processSinglePush(pushChannel[i])
}
}
func (task *Task) processSinglePush(ch chan *PushParams) {
var arg *PushParams
for {
arg = <-ch
task.pushSingleToConnect(arg.ServerId, arg.UserId, arg.Msg)
}
}
func (task *Task) Push(msg string) {
m := &proto.RedisMsg{}
if err := json.Unmarshal([]byte(msg), m); err != nil {
logrus.Infof(" json.Unmarshal err:%v ", err)
}
logrus.Infof("push msg info %s", m)
switch m.Op {
case config.OpSingleSend:
pushChannel[rand.Int()%config.Conf.Task.TaskBase.PushChan] <- &PushParams{
ServerId: m.ServerId,
UserId: m.UserId,
Msg: m.Msg,
}
case config.OpRoomSend:
task.broadcastRoomToConnect(m.RoomId, m.Msg)
case config.OpRoomCountSend:
task.broadcastRoomCountToConnect(m.RoomId, m.Count)
case config.OpRoomInfoSend:
task.broadcastRoomInfoToConnect(m.RoomId, m.RoomUserInfo)
}
}