-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (105 loc) · 3.03 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"context"
"encoding/json"
"github.com/DeanThompson/ginpprof"
"github.com/gin-gonic/gin"
"github.com/liumingmin/boxworld/constant"
"github.com/liumingmin/goutils/log"
"github.com/liumingmin/goutils/ws"
)
func main() {
ws.InitServer()
e := gin.Default()
e.Static("/static", "./static")
e.GET("/join", JoinGame)
ginpprof.WrapGroup(&e.RouterGroup)
e.Run(":8003")
}
func JoinGame(ctx *gin.Context) {
connMeta := ws.ConnectionMeta{
UserId: ctx.DefaultQuery("uid", "0000"),
Typed: 0,
DeviceId: "",
Version: 0,
Charset: 0,
}
_, err := ws.AcceptGin(ctx, connMeta,
ws.ConnEstablishHandlerOption(ConnFinished),
ws.ConnClosedHandlerOption(DisconnFinished),
)
if err != nil {
log.Error(ctx, "Accept client connection failed. error: %v", err)
return
}
}
func ConnFinished(ctx context.Context, conn ws.IConnection) {
d, _ := json.Marshal([]string{conn.UserId()})
uids := make([]string, 0)
ws.ClientConnHub.RangeConnsByFunc(func(s string, connection *ws.Connection) bool {
if conn.UserId() == connection.UserId() {
return true
}
uids = append(uids, connection.UserId())
return true
})
ws.ClientConnHub.RangeConnsByFunc(func(s string, connection *ws.Connection) bool {
if conn.UserId() == connection.UserId() {
d2, _ := json.Marshal(uids)
packet2 := ws.GetPoolMessage(constant.PROT_JOIN_GAME)
packet2.SetData(d2)
connection.SendMsg(context.Background(), packet2, nil)
return true
}
packet := ws.GetPoolMessage(constant.PROT_JOIN_GAME)
packet.SetData(d)
connection.SendMsg(context.Background(), packet, nil)
return true
})
}
func DisconnFinished(ctx context.Context, conn ws.IConnection) {
d, _ := json.Marshal([]string{conn.UserId()})
ws.ClientConnHub.RangeConnsByFunc(func(s string, connection *ws.Connection) bool {
if conn.UserId() == connection.UserId() {
return true
}
packet := ws.GetPoolMessage(constant.PROT_LEAVE_GAME)
packet.SetData(d)
connection.SendMsg(context.Background(), packet, nil)
return true
})
}
type PlayerPos struct {
Id string `json:"id"`
X float64 `json:"x"`
Y float64 `json:"y"`
Fx int `json:"fx"`
Vx float64 `json:"vx"`
Vy float64 `json:"vy"`
Dn int `json:"dn"`
Ani string `json:"ani"`
}
func sendPlayerPosToClients(ctx context.Context, movedPlayer *PlayerPos) {
d, _ := json.Marshal([]PlayerPos{*movedPlayer})
packet := ws.NewMessage(constant.PROT_PLAYER_POS)
packet.SetData(d)
ws.ClientConnHub.RangeConnsByFunc(func(s string, connection *ws.Connection) bool {
if movedPlayer.Id == connection.UserId() {
return true
}
//packet := ws.GetPoolMessage(constant.PROT_PLAYER_POS)
connection.SendMsg(ctx, packet, nil)
return true
})
}
func updatePlayerPos(ctx context.Context, conn ws.IConnection, msg ws.IMessage) error {
playerPos := PlayerPos{}
json.Unmarshal(msg.GetData(), &playerPos)
playerPos.Id = conn.UserId()
//conn.SetCommDataValue("pos", playerPos)
sendPlayerPosToClients(ctx, &playerPos)
return nil
}
func init() {
ws.RegisterHandler(constant.PROT_PLAYER_POS, updatePlayerPos)
}