Skip to content

Commit e358b94

Browse files
author
hero
committed
添加推送服务
1 parent 7a6a47f commit e358b94

File tree

7 files changed

+56
-3
lines changed

7 files changed

+56
-3
lines changed

go_push/gateway/http_handle.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package gateway
2+
3+

go_push/gateway/http_server.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package gateway
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"time"
7+
)
8+
9+
func InitHttpServer() {
10+
mux := http.NewServeMux()
11+
mux.HandleFunc("/push/all", getConn)
12+
mux.HandleFunc("/push/room", getConn)
13+
// HTTP服务
14+
server := http.Server{
15+
Addr: "0.0.0.0:9999",
16+
ReadTimeout: time.Duration(10) * time.Millisecond,
17+
WriteTimeout: time.Duration(10) * time.Millisecond,
18+
Handler: mux,
19+
}
20+
fmt.Println("启动HTTP服务器成功 :",9999)
21+
_ = server.ListenAndServe()
22+
}

go_push/gateway/push_job.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,20 @@ type PushJob struct {
77
}
88

99
type PushTask struct {
10-
10+
JobChan []chan *PushJob
11+
DistributionTask chan *PushJob
12+
}
13+
14+
func NewPushTask() *PushTask {
15+
var p *PushTask
16+
p.DistributionTask = make(chan *PushJob, 10)
17+
p.JobChan = make([]chan *PushJob, len(roomTitle)) //一个房间对应一个job
18+
GetRoomManage().AllRoom.Range(func(key, value interface{}) bool {
19+
return true
20+
})
21+
return p
22+
}
23+
24+
func (p *PushTask) distributionTask() {
25+
1126
}

go_push/gateway/room.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import "sync"
55
//一个房间代表一个订阅推送类型
66
type Room struct {
77
id string
8+
title string
89
RConn sync.Map
910
}
1011

11-
func NewRoom(id string) *Room {
12+
func NewRoom(id,title string) *Room {
1213
return &Room{
1314
id: id,
15+
title:title,
1416
}
1517
}
1618

go_push/gateway/room_manage.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ func (r *RoomManage) DelConn(ws *WsConnection) {
3131
}
3232

3333
func (r *RoomManage) AddRoom(id string, ws *WsConnection) error {
34+
var room *Room
3435
val, ok := r.AllRoom.Load(id)
3536
if !ok {
3637
//room = NewRoom(id)
3738
//r.AllRoom.Store(id, room)
3839
return errors.New("not find room")
3940
}
40-
room := val.(*Room)
41+
room = val.(*Room)
4142
room.JoinRoom(ws)
4243
return nil
4344
}

go_push/gateway/ws_server.go

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

33
import (
4+
"fmt"
45
"github.com/gorilla/websocket"
56
"net"
67
"net/http"
8+
"strconv"
79
"strings"
810
"time"
911
)
@@ -14,18 +16,25 @@ var wsUpgrader = websocket.Upgrader{
1416
return true
1517
},
1618
}
19+
var roomTitle = []string{"健身", "体育", "电影", "音乐"}
1720

1821
func InitWsServer() {
1922
mux := http.NewServeMux()
2023
mux.HandleFunc("/connect", getConn)
2124
NewRoomManage()
25+
//初始化房间
26+
for i := 0; i < 4; i++ {
27+
NewRoom(strconv.Itoa(i), roomTitle[i])
28+
fmt.Println("新建房间推送类型", roomTitle[i])
29+
}
2230
// HTTP服务
2331
server := http.Server{
2432
Addr: "0.0.0.0:8888",
2533
ReadTimeout: time.Duration(10) * time.Millisecond,
2634
WriteTimeout: time.Duration(10) * time.Millisecond,
2735
Handler: mux,
2836
}
37+
fmt.Println("启动HTTP服务器成功 :", 8888)
2938
_ = server.ListenAndServe()
3039

3140
}

go_push/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ import "learning_tools/go_push/gateway"
44

55
func main() {
66
gateway.InitWsServer()
7+
gateway.InitHttpServer()
78
}

0 commit comments

Comments
 (0)