// 发送封号信息 // clientObj:客户端对象 func SendForbidMsg(clientObj *client.Client) { responseObj := responseDataObject.NewSocketResponseObject(commandType.Login) responseObj.SetResultStatus(responseDataObject.PlayerIsForbidden) // 先发送消息,然后再断开连接 responseResult(clientObj, responseObj) clientObj.LogoutAndQuit() }
// 发送在另一台设备登陆的信息 // clientObj:客户端对象 func SendLoginAnotherDeviceMsg(clientObj *client.Client) { responseObj := responseDataObject.NewSocketResponseObject(commandType.Login) responseObj.SetResultStatus(responseDataObject.LoginOnAnotherDevice) // 先发送消息,然后再断开连接 responseResult(clientObj, responseObj) clientObj.LogoutAndQuit() }
// 发送响应结果 // clientObj:客户端对象 // responseObject:响应对象 func responseResult(clientObj *client.Client, responseObj *responseDataObject.SocketResponseObject) { b, err := json.Marshal(responseObj) if err != nil { logUtil.Log(fmt.Sprintf("序列化输出结果%v出错", responseObj), logUtil.Error, true) } else { clientObj.SendByteMessage(b) } }
// 更新登录信息 // playerObj:玩家对象 // clientObj:客户端对象 // isNewPlayer:是否是新玩家 func UpdateLoginInfo(playerObj *player.Player, clientObj *client.Client, isNewPlayer bool) error { playerObj.ClientId = clientObj.Id() playerObj.LoginTime = time.Now() // 如果不是新玩家则更新登录时间,否则使用创建时指定的登录时间 if !isNewPlayer { if err := playerDAL.UpdateLoginTime(playerObj); err != nil { return err } } return nil }
// 处理客户端逻辑 // clientObj:客户端对象 func handleClientContent(clientObj *client.Client) { for { content, ok := clientObj.GetValidMessage() if !ok { break } // 处理数据,如果长度为0则表示心跳包 if len(content) == 0 { continue } else { chatBLL.HanleRequest(clientObj, content) } } }
// 根据客户端对象来断开连接 // 注销客户端连接 // 从缓存中移除玩家对象 // clientObj:客户端对象 // clinetDisconnectType:客户端断开连接的类型;如果是来自于rpc则意味着之前客户端已经关闭连接,现在需要将客户端对象从缓存中移除了;否则是客户端过期,需要关闭 func DisconnectByClient(clientObj *client.Client, clinetDisconnectType disconnectType.ClientDisconnectType) { // 将玩家从缓存中移除 if clientObj.PlayerId() != "" { if playerObj, exists, err := GetPlayer(clientObj.PlayerId(), false); err == nil && exists { UnRegisterPlayer(playerObj) } } // 注销客户端连接,并从缓存中移除 switch clinetDisconnectType { case disconnectType.FromExpire: clientObj.LogoutAndQuit() case disconnectType.FromRpc: clientBLL.UnRegisterClient(clientObj) } }