// 发送在另一台设备登陆的信息 // clientObj:客户端对象 func SendLoginAnotherDeviceMsg(clientObj *client.Client) { responseObj := responseDataObject.NewSocketResponseObject(commandType.Login) responseObj.SetResultStatus(responseDataObject.LoginOnAnotherDevice) // 先发送消息,然后再断开连接 responseResult(clientObj, responseObj) clientObj.LogoutAndQuit() }
// 发送封号信息 // clientObj:客户端对象 func SendForbidMsg(clientObj *client.Client) { responseObj := responseDataObject.NewSocketResponseObject(commandType.Login) responseObj.SetResultStatus(responseDataObject.PlayerIsForbidden) // 先发送消息,然后再断开连接 responseResult(clientObj, responseObj) clientObj.LogoutAndQuit() }
// 服务器推送信息 // message:推送的消息 func PushMessage(message string) { responseObj := responseDataObject.NewSocketResponseObject(commandType.SendMessage) // 将message反序列化 messageMap := make(map[string]interface{}) if err := json.Unmarshal([]byte(message), &messageMap); err != nil { return } // 提取PlayerIds字段,用于查询对应的玩家对象 playerList := make([]*player.Player, 0, 32) // 判断是否发给指定玩家;如果不是发给指定玩家,则判断是否发给指定公会 if playerIds, exists := messageMap["PlayerIds"]; exists { if playerIdArray, ok := playerIds.([]interface{}); !ok { return } else { for _, playerId := range playerIdArray { if playerIdStr, ok := playerId.(string); !ok { continue } else { if playerObj, exists, err := GetPlayer(playerIdStr, false); err != nil || exists == false { continue } else { playerList = append(playerList, playerObj) } } } } } else { // 判断是否发给指定公会;如果不是发给指定公会,则发给所有玩家 if unionId, exists := messageMap["UnionId"]; exists { if unionId_str, ok := unionId.(string); ok { playerList = GetPlayerListInUnion(unionId_str) } } else { playerList = GetPlayerList() } } // 如果messageMap中存在PlayerIds或UnionId字段,则将PlayerIds、UnionId移除,并再进行序列化 _, playerIdsExists := messageMap["PlayerIds"] _, unionIdExists := messageMap["UnionId"] if playerIdsExists || unionIdExists { delete(messageMap, "PlayerIds") delete(messageMap, "UnionId") if b, err := json.Marshal(messageMap); err == nil { message = string(b) } } // 设置responseObj的Data属性(先还原到旧方式,等下次再更新) responseObj.SetData(chatMessage.NewResponseData(channelType.System, message, nil, nil)) // 发送给所有玩家数据 SendToPlayer(playerList, responseObj) }