From ab6215d61117fb2b278a8429fa31d7baad087455 Mon Sep 17 00:00:00 2001 From: Lex Lim Date: Thu, 15 Dec 2022 17:28:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=91=BD=E5=90=8D=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=8A=A8=E5=A1=AB=E5=86=99=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E5=99=A8=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/ClientSocketController.go | 13 +++++++------ go.mod | 1 + go.sum | 2 ++ store/model.go | 11 +++++------ utils/utils.go | 19 ++++++++++++++++++- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/controller/ClientSocketController.go b/controller/ClientSocketController.go index 8a36fea..df0f1f3 100644 --- a/controller/ClientSocketController.go +++ b/controller/ClientSocketController.go @@ -126,10 +126,10 @@ func (c *ClientSocketController) SetToken(socket socketio.Conn, token string) { return } - if playerInfo.BindingUser != "" { - ctx.User = playerInfo.BindingUser - userName := playerInfo.BindingUser - userInfo := c.storeModel.GetUserInfo(playerInfo.BindingUser) + if playerInfo.BoundUser != "" { + ctx.User = playerInfo.BoundUser + userName := playerInfo.BoundUser + userInfo := c.storeModel.GetUserInfo(playerInfo.BoundUser) if userInfo != nil { userName = userInfo.DisplayName } @@ -137,7 +137,7 @@ func (c *ClientSocketController) SetToken(socket socketio.Conn, token string) { // 通知客户端 socket.Emit("user:update", ctx.User, userName) // 加入房间 - c.SwitchRoom(socket, "user", playerInfo.BindingUser) + c.SwitchRoom(socket, "user", playerInfo.BoundUser) } if playerInfo.PairCode != "" { // 删除存储中的code @@ -168,6 +168,7 @@ func (c *ClientSocketController) RefreshToken(socket socketio.Conn) { if playerInfo == nil { playerInfo = new(store.PlayerInfo) + playerInfo.Name = utils.GetPlayerNameFromUserAgent(socket.RemoteHeader().Get("User-Agent")) } else { c.storeModel.RemovePlayerInfo(oldToken) } @@ -284,7 +285,7 @@ func (c *ClientSocketController) OnBindUser(eventObj ...interface{}) { // 存储信息 userInfo.BoundPlayer = append(userInfo.BoundPlayer, event.Token) - playerInfo.BindingUser = event.User + playerInfo.BoundUser = event.User c.storeModel.SetUserInfo(event.User, userInfo) c.storeModel.SetPlayerInfo(event.Token, playerInfo) diff --git a/go.mod b/go.mod index cda7b2c..ee1438b 100644 --- a/go.mod +++ b/go.mod @@ -39,6 +39,7 @@ require ( github.com/magiconair/properties v1.8.6 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect + github.com/mileusna/useragent v1.2.1 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml v1.9.5 // indirect diff --git a/go.sum b/go.sum index 655d3f8..dd98008 100644 --- a/go.sum +++ b/go.sum @@ -227,6 +227,8 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9 github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/mileusna/useragent v1.2.1 h1:p3RJWhi3LfuI6BHdddojREyK3p6qX67vIfOVMnUIVr0= +github.com/mileusna/useragent v1.2.1/go.mod h1:3d8TOmwL/5I8pJjyVDteHtgDGcefrFUX4ccGOMKNYYc= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= diff --git a/store/model.go b/store/model.go index 1617813..a9316ff 100644 --- a/store/model.go +++ b/store/model.go @@ -3,10 +3,9 @@ package store import ( "context" "encoding/json" - "log" - "github.com/eko/gocache/lib/v4/cache" "github.com/eko/gocache/lib/v4/store" + "log" ) type StoreModel struct { @@ -15,10 +14,10 @@ type StoreModel struct { } type PlayerInfo struct { - IsOnline bool `json:"online"` - BindingUser string `json:"binding_user"` - PairCode string `json:"pair_code"` - Name string `json:"name"` + IsOnline bool `json:"online"` + BoundUser string `json:"bound_user"` + PairCode string `json:"pair_code"` + Name string `json:"name"` } type UserInfo struct { diff --git a/utils/utils.go b/utils/utils.go index f11aef3..f2acfc7 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,6 +1,9 @@ package utils -import "strings" +import ( + "github.com/mileusna/useragent" + "strings" +) func AddAsterisks(text string, prefixLen int, suffixLen int) string { strLen := len(text) @@ -10,3 +13,17 @@ func AddAsterisks(text string, prefixLen int, suffixLen int) string { } return text[0:prefixLen] + strings.Repeat("*", padLen) + text[strLen-suffixLen:strLen] } + +func GetPlayerNameFromUserAgent(userAgentString string) string { + ua := useragent.Parse(userAgentString) + name := ua.OS + if ua.Mobile { + name += " 手机" + } else if ua.Tablet { + name += " 平板电脑" + } else if ua.Desktop { + name += " 电脑" + } + name += "上的 " + ua.Name + return name +}