Compare commits
2 Commits
5af027b35c
...
a3eb63eea6
| Author | SHA1 | Date | |
|---|---|---|---|
| a3eb63eea6 | |||
| 9e1889b956 |
2
go.mod
2
go.mod
@@ -4,7 +4,7 @@ go 1.23.1
|
||||
|
||||
require (
|
||||
bou.ke/monkey v1.0.2
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260108144540-69e82ec0fe77
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260110153008-0be254c6a298
|
||||
github.com/alicebob/miniredis/v2 v2.35.0
|
||||
github.com/gin-contrib/cors v1.7.6
|
||||
github.com/gin-gonic/gin v1.11.0
|
||||
|
||||
6
go.sum
6
go.sum
@@ -2,10 +2,8 @@ bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
|
||||
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
|
||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260107061047-e02e56781e78 h1:4CP8w5jHDOdN7hym8YrtioxNDd7PS2WcZDrO6dtY344=
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260107061047-e02e56781e78/go.mod h1:xv6m1I2jUA6mudKVznygpnzMoshBQarthHD1QnkW4qc=
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260108144540-69e82ec0fe77 h1:39SVfV3+uysM25P+dSnJMvtJJI9l2aZ+/n/ooTdHqxk=
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260108144540-69e82ec0fe77/go.mod h1:xv6m1I2jUA6mudKVznygpnzMoshBQarthHD1QnkW4qc=
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260110153008-0be254c6a298 h1:zKQC0gqQfP9sUVYs7OsoHSrzdk379u6aenko6rMx1go=
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260110153008-0be254c6a298/go.mod h1:xv6m1I2jUA6mudKVznygpnzMoshBQarthHD1QnkW4qc=
|
||||
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
||||
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI=
|
||||
|
||||
@@ -18,6 +18,7 @@ const (
|
||||
// PublicPaths 不需要鉴权的接口,硬编码注册
|
||||
var PublicPaths = []string{
|
||||
"/user/info",
|
||||
"/user/generate_question",
|
||||
}
|
||||
|
||||
var (
|
||||
|
||||
@@ -24,7 +24,7 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error {
|
||||
if args, err := server.Recv(); err != nil {
|
||||
return
|
||||
} else {
|
||||
if args.USN == -1 {
|
||||
if args.USN == "" {
|
||||
|
||||
//utils.WorkerPool(ws_handler.UserMgr.GetAllInterface(), func(task interface{}) {
|
||||
// client := task.(*ws_handler.Client)
|
||||
|
||||
@@ -23,7 +23,7 @@ type sceneStream struct {
|
||||
stream grpc.ClientStream
|
||||
}
|
||||
|
||||
func findSceneBySID(sid int64, fun SceneFun) (*sceneStream, error) {
|
||||
func findSceneBySID(sid string, fun SceneFun) (*sceneStream, error) {
|
||||
key := sceneKey(sid, fun)
|
||||
|
||||
if v, ok := sceneServer.Load(key); ok {
|
||||
@@ -54,7 +54,7 @@ func findSceneBySID(sid int64, fun SceneFun) (*sceneStream, error) {
|
||||
return ss, nil
|
||||
}
|
||||
|
||||
func SendMessageToScene(sid int64, fun SceneFun, msg proto.Message, re ...bool) error {
|
||||
func SendMessageToScene(sid string, fun SceneFun, msg proto.Message, re ...bool) error {
|
||||
ss, err := findSceneBySID(sid, fun)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -79,6 +79,6 @@ func SendMessageToScene(sid int64, fun SceneFun, msg proto.Message, re ...bool)
|
||||
return nil
|
||||
}
|
||||
|
||||
func sceneKey(sid int64, fun SceneFun) string {
|
||||
return strconv.FormatInt(sid, 10) + "-" + strconv.Itoa(int(fun))
|
||||
func sceneKey(sid string, fun SceneFun) string {
|
||||
return sid + "-" + strconv.Itoa(int(fun))
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ type LoginReq struct {
|
||||
}
|
||||
|
||||
type LoginResp struct {
|
||||
USN int64 `json:"usn"`
|
||||
USN string `json:"usn"`
|
||||
Name string `json:"name"`
|
||||
AccessToken string `json:"accessToken"`
|
||||
RefreshToken string `json:"refreshToken"`
|
||||
@@ -97,7 +97,7 @@ func RefreshToken(c *gin.Context) {
|
||||
}))
|
||||
}
|
||||
|
||||
func genToken(ctx context.Context, usn int64) (string, string, error) {
|
||||
func genToken(ctx context.Context, usn string) (string, string, error) {
|
||||
at, err := genTokenOne(ctx, global.KeyGatewayAccessToken, usn, 2*time.Hour)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
@@ -109,7 +109,7 @@ func genToken(ctx context.Context, usn int64) (string, string, error) {
|
||||
return at, rt, nil
|
||||
}
|
||||
|
||||
func genTokenOne(ctx context.Context, key string, usn int64, ttl time.Duration) (string, error) {
|
||||
func genTokenOne(ctx context.Context, key string, usn string, ttl time.Duration) (string, error) {
|
||||
token, err := utils.GenToken(usn, config.Get().Auth.Secret, time.Duration(config.Get().Auth.Expire)*time.Second)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var GatewaySID int64
|
||||
var GatewaySID string
|
||||
|
||||
type Client struct {
|
||||
sync.WaitGroup
|
||||
@@ -24,14 +24,14 @@ type Client struct {
|
||||
cancel context.CancelFunc // 取消上下文
|
||||
heartBeat time.Time // 最后一次心跳
|
||||
|
||||
Status int32 // 状态:0 登陆中 1 正常 2 离线
|
||||
USN int64 // 用户ID
|
||||
SceneSID int64 // 场景服ID
|
||||
InstanceID int32 // 副本ID,副本类型
|
||||
UniqueNo int64 // 副本唯一编号
|
||||
Status int32 // 状态:0 登陆中 1 正常 2 离线
|
||||
USN string // 用户ID
|
||||
SceneSID string // 场景服ID
|
||||
InstanceID int32 // 副本ID,副本类型
|
||||
UniqueNo string // 副本唯一编号
|
||||
}
|
||||
|
||||
func NewClient(usn int64, conn socket.ISocketConn) *Client {
|
||||
func NewClient(usn string, conn socket.ISocketConn) *Client {
|
||||
client := &Client{
|
||||
USN: usn,
|
||||
conn: conn,
|
||||
|
||||
@@ -76,7 +76,7 @@ func (c *Client) onEnter(msg *sc_pb.C2S_EnterInstance) {
|
||||
}
|
||||
|
||||
func (c *Client) onLeave() {
|
||||
if c.SceneSID == 0 {
|
||||
if c.SceneSID == "" {
|
||||
return
|
||||
}
|
||||
client, err := service.SceneNewClient(c.SceneSID)
|
||||
@@ -95,7 +95,7 @@ func (c *Client) onLeave() {
|
||||
}
|
||||
|
||||
func (c *Client) onAction(msg *sc_pb.C2S_Action) {
|
||||
if c.SceneSID == 0 {
|
||||
if c.SceneSID == "" {
|
||||
return
|
||||
}
|
||||
if err := stream_client.SendMessageToScene(c.SceneSID, stream_client.FunAction, &grpc_pb.ActionReq{
|
||||
|
||||
@@ -8,35 +8,35 @@ import (
|
||||
var UserMgr *userManager
|
||||
|
||||
type userManager struct {
|
||||
userMap map[int64]*Client
|
||||
userMap map[string]*Client
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
UserMgr = &userManager{
|
||||
userMap: make(map[int64]*Client),
|
||||
userMap: make(map[string]*Client),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *userManager) Add(usn int64, client *Client) {
|
||||
func (m *userManager) Add(usn string, client *Client) {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
m.userMap[usn] = client
|
||||
global.OnlineUsersGauge.Inc()
|
||||
}
|
||||
|
||||
func (m *userManager) Delete(usn int64) {
|
||||
func (m *userManager) Delete(usn string) {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
delete(m.userMap, usn)
|
||||
global.OnlineUsersGauge.Dec()
|
||||
}
|
||||
|
||||
func (m *userManager) GetAll() map[int64]*Client {
|
||||
func (m *userManager) GetAll() map[string]*Client {
|
||||
m.RLock()
|
||||
defer m.RUnlock()
|
||||
|
||||
copyMap := make(map[int64]*Client, len(m.userMap))
|
||||
copyMap := make(map[string]*Client, len(m.userMap))
|
||||
for k, v := range m.userMap {
|
||||
copyMap[k] = v
|
||||
}
|
||||
@@ -54,7 +54,7 @@ func (m *userManager) GetAllInterface() []interface{} {
|
||||
return r
|
||||
}
|
||||
|
||||
func (m *userManager) GetByUSN(usn int64) *Client {
|
||||
func (m *userManager) GetByUSN(usn string) *Client {
|
||||
m.RLock()
|
||||
defer m.RUnlock()
|
||||
return m.userMap[usn]
|
||||
|
||||
@@ -161,7 +161,7 @@ func (l *Login) CheckOnline(user *User) string {
|
||||
}
|
||||
|
||||
// KickUser 把玩家踢下线
|
||||
func (l *Login) KickUser(gatewaySID int64, usn int64) bool {
|
||||
func (l *Login) KickUser(gatewaySID string, usn string) bool {
|
||||
gc, err := service.GatewayNewClient(gatewaySID)
|
||||
if err != nil {
|
||||
log.Errorf("KickUser cannot find gateway client: %v, sid: %v", err, gatewaySID)
|
||||
|
||||
@@ -78,7 +78,7 @@ func (q *QueueUp) Dequeue() (*client.Client, error) {
|
||||
}
|
||||
|
||||
// GetPosition 返回用户前面还有多少人在排队
|
||||
func (q *QueueUp) GetPosition(usn int64) (int64, bool) {
|
||||
func (q *QueueUp) GetPosition(usn string) (int64, bool) {
|
||||
val, ok := q.waiting.Load(usn)
|
||||
if !ok {
|
||||
return 0, false
|
||||
@@ -88,7 +88,7 @@ func (q *QueueUp) GetPosition(usn int64) (int64, bool) {
|
||||
}
|
||||
|
||||
// RemoveUser 安全移除用户(标记为取消)
|
||||
func (q *QueueUp) RemoveUser(usn int64) bool {
|
||||
func (q *QueueUp) RemoveUser(usn string) bool {
|
||||
_, loaded := q.waiting.LoadAndDelete(usn)
|
||||
return loaded
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -73,7 +72,7 @@ func authJwt() gin.HandlerFunc {
|
||||
}
|
||||
|
||||
// 这里将Header写到请求中,grpc-gateway框架会读取然后传给grpc服务
|
||||
c.Request.Header.Set("X-Usn", strconv.Itoa(int(claims.USN)))
|
||||
c.Request.Header.Set("X-Usn", claims.USN)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ func (g *GatewayWsServer) OnHandShake(conn socket.ISocketConn) socket.Action {
|
||||
|
||||
func (g *GatewayWsServer) OnMessage(conn socket.ISocketConn, bytes []byte) socket.Action {
|
||||
cli, ok := conn.GetParam("client").(*client.Client)
|
||||
if !ok || cli.USN == 0 || cli.Status != 1 {
|
||||
if !ok || cli.USN == "" || cli.Status != 1 {
|
||||
return socket.Close
|
||||
}
|
||||
cli.OnEvent(&client.ClientEvent{Msg: bytes})
|
||||
@@ -53,7 +53,7 @@ func (g *GatewayWsServer) OnMessage(conn socket.ISocketConn, bytes []byte) socke
|
||||
|
||||
func (g *GatewayWsServer) OnPong(conn socket.ISocketConn) {
|
||||
cli, ok := conn.GetParam("client").(*client.Client)
|
||||
if !ok || cli.USN == 0 {
|
||||
if !ok || cli.USN == "" {
|
||||
return
|
||||
}
|
||||
cli.OnEvent(&client.PongEvent{})
|
||||
|
||||
Reference in New Issue
Block a user