|
|
@ -25,6 +25,9 @@ type ClientSocketController struct {
|
|
|
|
|
|
|
|
|
|
|
|
var clientSocketControllerInstance *ClientSocketController
|
|
|
|
var clientSocketControllerInstance *ClientSocketController
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var ErrPlayerNotExists = errors.New("code not exists")
|
|
|
|
|
|
|
|
var ErrParamsInvalid = errors.New("params invalid")
|
|
|
|
|
|
|
|
|
|
|
|
func NewClientSocketController(basePath string, ctx *context.ServerContext) (*ClientSocketController, error) {
|
|
|
|
func NewClientSocketController(basePath string, ctx *context.ServerContext) (*ClientSocketController, error) {
|
|
|
|
instance := &ClientSocketController{
|
|
|
|
instance := &ClientSocketController{
|
|
|
|
SocketController{
|
|
|
|
SocketController{
|
|
|
@ -51,6 +54,7 @@ func (c *ClientSocketController) Initialize() error {
|
|
|
|
|
|
|
|
|
|
|
|
server.OnConnect(c.basePath, func(s socketio.Conn) error {
|
|
|
|
server.OnConnect(c.basePath, func(s socketio.Conn) error {
|
|
|
|
s.SetContext(&ClientSocketContext{})
|
|
|
|
s.SetContext(&ClientSocketContext{})
|
|
|
|
|
|
|
|
s.Join("clients")
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
@ -69,6 +73,12 @@ func (c *ClientSocketController) Initialize() error {
|
|
|
|
log.Println("meet error:", e)
|
|
|
|
log.Println("meet error:", e)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c.events.AddListener("bindUser", c.OnBindUser)
|
|
|
|
|
|
|
|
c.events.AddListener("playMusic", c.OnPlayMusic)
|
|
|
|
|
|
|
|
c.events.AddListener("playSoundEffect", c.OnPlaySoundEffect)
|
|
|
|
|
|
|
|
c.events.AddListener("playVoice", c.OnPlayVoice)
|
|
|
|
|
|
|
|
c.events.AddListener("stopPlay", c.OnStopPlay)
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -89,49 +99,21 @@ func (c *ClientSocketController) SwitchRoom(socket socketio.Conn, prefix string,
|
|
|
|
func (c *ClientSocketController) OnDisconnect(socket socketio.Conn, reason string) {
|
|
|
|
func (c *ClientSocketController) OnDisconnect(socket socketio.Conn, reason string) {
|
|
|
|
ctx := GetContext[ClientSocketContext](socket)
|
|
|
|
ctx := GetContext[ClientSocketContext](socket)
|
|
|
|
// 删除存储中的code
|
|
|
|
// 删除存储中的code
|
|
|
|
|
|
|
|
if ctx.Token != "" {
|
|
|
|
if ctx.PairCode != "" {
|
|
|
|
if ctx.PairCode != "" {
|
|
|
|
c.storeModel.RemoveCodeInfo(ctx.PairCode)
|
|
|
|
c.storeModel.RemoveCodeInfo(ctx.PairCode)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ctx.Token != "" {
|
|
|
|
|
|
|
|
playerInfo := c.storeModel.GetPlayerInfo(ctx.Token)
|
|
|
|
playerInfo := c.storeModel.GetPlayerInfo(ctx.Token)
|
|
|
|
if playerInfo != nil {
|
|
|
|
if playerInfo != nil {
|
|
|
|
playerInfo.PairCode = ""
|
|
|
|
playerInfo.PairCode = ""
|
|
|
|
|
|
|
|
playerInfo.IsOnline = false
|
|
|
|
c.storeModel.SetPlayerInfo(ctx.Token, playerInfo)
|
|
|
|
c.storeModel.SetPlayerInfo(ctx.Token, playerInfo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// RefreshToken 刷新token
|
|
|
|
|
|
|
|
func (c *ClientSocketController) RefreshToken(socket socketio.Conn) {
|
|
|
|
|
|
|
|
ctx := GetContext[ClientSocketContext](socket)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
oldToken := ctx.Token
|
|
|
|
|
|
|
|
var playerInfo *store.PlayerInfo
|
|
|
|
|
|
|
|
if oldToken != "" {
|
|
|
|
|
|
|
|
playerInfo = c.storeModel.GetPlayerInfo(oldToken)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newToken := randstr.RandomAlphanumeric(32)
|
|
|
|
|
|
|
|
ctx.Token = newToken
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if playerInfo == nil {
|
|
|
|
// SetToken 设置token (客户端登录)
|
|
|
|
playerInfo = new(store.PlayerInfo)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
c.storeModel.RemovePlayerInfo(oldToken)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if playerInfo.PairCode != "" { // 删除存储中的code
|
|
|
|
|
|
|
|
c.storeModel.RemoveCodeInfo(playerInfo.PairCode)
|
|
|
|
|
|
|
|
playerInfo.PairCode = ""
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c.storeModel.SetPlayerInfo(newToken, playerInfo)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
socket.Emit("token:update", newToken)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SetToken 设置token
|
|
|
|
|
|
|
|
func (c *ClientSocketController) SetToken(socket socketio.Conn, token string) {
|
|
|
|
func (c *ClientSocketController) SetToken(socket socketio.Conn, token string) {
|
|
|
|
ctx := GetContext[ClientSocketContext](socket)
|
|
|
|
ctx := GetContext[ClientSocketContext](socket)
|
|
|
|
|
|
|
|
|
|
|
@ -153,7 +135,7 @@ func (c *ClientSocketController) SetToken(socket socketio.Conn, token string) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 通知客户端
|
|
|
|
// 通知客户端
|
|
|
|
socket.Emit("user:update", userName)
|
|
|
|
socket.Emit("user:update", ctx.User, userName)
|
|
|
|
// 加入房间
|
|
|
|
// 加入房间
|
|
|
|
c.SwitchRoom(socket, "user", playerInfo.BindingUser)
|
|
|
|
c.SwitchRoom(socket, "user", playerInfo.BindingUser)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -164,10 +146,42 @@ func (c *ClientSocketController) SetToken(socket socketio.Conn, token string) {
|
|
|
|
c.storeModel.SetPlayerInfo(ctx.Token, playerInfo)
|
|
|
|
c.storeModel.SetPlayerInfo(ctx.Token, playerInfo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
playerInfo.IsOnline = true
|
|
|
|
|
|
|
|
c.storeModel.SetPlayerInfo(ctx.Token, playerInfo)
|
|
|
|
|
|
|
|
|
|
|
|
socket.Emit("token:update", token)
|
|
|
|
socket.Emit("token:update", token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// RefreshToken 刷新token
|
|
|
|
|
|
|
|
func (c *ClientSocketController) RefreshToken(socket socketio.Conn) {
|
|
|
|
|
|
|
|
ctx := GetContext[ClientSocketContext](socket)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
oldToken := ctx.Token
|
|
|
|
|
|
|
|
var playerInfo *store.PlayerInfo
|
|
|
|
|
|
|
|
if oldToken != "" {
|
|
|
|
|
|
|
|
playerInfo = c.storeModel.GetPlayerInfo(oldToken)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newToken := randstr.RandomAlphanumeric(32)
|
|
|
|
|
|
|
|
ctx.Token = newToken
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if playerInfo == nil {
|
|
|
|
|
|
|
|
playerInfo = new(store.PlayerInfo)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
c.storeModel.RemovePlayerInfo(oldToken)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if playerInfo.PairCode != "" { // 删除存储中的code
|
|
|
|
|
|
|
|
c.storeModel.RemoveCodeInfo(playerInfo.PairCode)
|
|
|
|
|
|
|
|
playerInfo.PairCode = ""
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c.storeModel.SetPlayerInfo(newToken, playerInfo)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
socket.Emit("token:update", newToken)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetCode 获取连接码
|
|
|
|
// GetCode 获取连接码
|
|
|
|
func (c *ClientSocketController) GetCode(socket socketio.Conn) {
|
|
|
|
func (c *ClientSocketController) GetCode(socket socketio.Conn) {
|
|
|
|
ctx := GetContext[ClientSocketContext](socket)
|
|
|
|
ctx := GetContext[ClientSocketContext](socket)
|
|
|
@ -223,36 +237,68 @@ func (c *ClientSocketController) PingbackStop(socket socketio.Conn, musicId stri
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BindPlayer 用户触发绑定播放器
|
|
|
|
// BindPlayer 用户触发绑定播放器
|
|
|
|
func (c *ClientSocketController) BindPlayer(user string, code string) (*store.PlayerInfo, error) {
|
|
|
|
func (c *ClientSocketController) BindPlayer(user string, code string) (string, error) {
|
|
|
|
// code为空则不绑定
|
|
|
|
// code为空则不绑定
|
|
|
|
if code == "" {
|
|
|
|
if code == "" {
|
|
|
|
return nil, errors.New("code invalid")
|
|
|
|
return "", ErrParamsInvalid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.sockets.ForEach("/", "", func(socket socketio.Conn) {
|
|
|
|
token := ""
|
|
|
|
|
|
|
|
c.sockets.ForEach("/", "clients", func(socket socketio.Conn) {
|
|
|
|
ctx := GetContext[ClientSocketContext](socket)
|
|
|
|
ctx := GetContext[ClientSocketContext](socket)
|
|
|
|
if ctx.PairCode == code {
|
|
|
|
if ctx.PairCode == code { // 找到需要绑定的客户端
|
|
|
|
// 找到需要绑定的客户端
|
|
|
|
token = ctx.Token
|
|
|
|
c.events.EmitEvent("bindUser", &events.BindUserEvent{
|
|
|
|
c.events.EmitEvent("bindUser", &events.BindUserEvent{
|
|
|
|
Token: ctx.Token,
|
|
|
|
Token: token,
|
|
|
|
User: user,
|
|
|
|
User: user,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
userName := user
|
|
|
|
if token != "" {
|
|
|
|
userInfo := c.storeModel.GetUserInfo(user)
|
|
|
|
return token, nil
|
|
|
|
if userInfo != nil {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return "", ErrPlayerNotExists
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *ClientSocketController) OnBindUser(eventObj ...interface{}) {
|
|
|
|
|
|
|
|
event, err := utils.GetEvent[events.BindUserEvent](eventObj)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Panicln("Cannot get event in OnBindUser:", err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
userName := event.User
|
|
|
|
|
|
|
|
userInfo := c.storeModel.GetUserInfo(event.User)
|
|
|
|
|
|
|
|
if userInfo == nil {
|
|
|
|
|
|
|
|
log.Println("Cannot find user info for user name '" + event.User + "' while bind player")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
userName = userInfo.DisplayName
|
|
|
|
userName = userInfo.DisplayName
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
playerInfo := c.storeModel.GetPlayerInfo(event.Token)
|
|
|
|
|
|
|
|
if playerInfo == nil {
|
|
|
|
|
|
|
|
log.Println("Cannot find player info for token '" + event.Token + "' while bind player")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 存储信息
|
|
|
|
|
|
|
|
userInfo.BoundPlayer = append(userInfo.BoundPlayer, event.Token)
|
|
|
|
|
|
|
|
playerInfo.BindingUser = event.User
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c.storeModel.SetUserInfo(event.User, userInfo)
|
|
|
|
|
|
|
|
c.storeModel.SetPlayerInfo(event.Token, playerInfo)
|
|
|
|
|
|
|
|
|
|
|
|
// 通知客户端
|
|
|
|
// 通知客户端
|
|
|
|
socket.Emit("user:update", userName)
|
|
|
|
c.sockets.ForEach("/", "", func(socket socketio.Conn) {
|
|
|
|
|
|
|
|
ctx := GetContext[ClientSocketContext](socket)
|
|
|
|
|
|
|
|
if ctx.Token == event.Token {
|
|
|
|
|
|
|
|
socket.Emit("user:update", event.User, userName)
|
|
|
|
|
|
|
|
|
|
|
|
// 加入房间
|
|
|
|
// 加入房间
|
|
|
|
c.SwitchRoom(socket, "user", user)
|
|
|
|
c.SwitchRoom(socket, "user", event.User)
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return nil, errors.New("code not exists")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// OnPlayMusic 用户触发播放音乐
|
|
|
|
// OnPlayMusic 用户触发播放音乐
|
|
|
@ -283,6 +329,17 @@ func (c *ClientSocketController) OnPlayMusic(eventObj ...interface{}) {
|
|
|
|
c.sockets.BroadcastToRoom("/", "user"+event.User, "music:play", res)
|
|
|
|
c.sockets.BroadcastToRoom("/", "user"+event.User, "music:play", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *ClientSocketController) OnPlaySE(eventObj interface{}) {
|
|
|
|
// OnPlaySoundEffect 用户触发播放音效
|
|
|
|
|
|
|
|
func (c *ClientSocketController) OnPlaySoundEffect(eventObj ...interface{}) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// OnPlayVoice 用户触发播放语音
|
|
|
|
|
|
|
|
func (c *ClientSocketController) OnPlayVoice(eventObj ...interface{}) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// OnStopPlay 用户触发停止播放
|
|
|
|
|
|
|
|
func (c *ClientSocketController) OnStopPlay(eventObj ...interface{}) {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|