Skip to content

服务器逻辑开发

服务器逻辑开发

1. 消息流程

1. 接收消息

    -- cmd:客户端发给服务器已从json转化为table
    -- laccount:为此玩家对应在go里面的对象
    Net.XXXXXXXX = function(cmd, laccount)
    end

可根据游戏需求,进行获取相应玩家属性信息:

local uid      = laccount.GetUid()           -- 获取玩家uid,            类型:uint64
local nickName = laccount.GetThirdNickName() -- 获取玩家第三方昵称,     类型:string 
local faceUrl  = laccount.GetThirdFaceUrl()  -- 获取玩家第三方头像,     类型:string
local platId   = laccount.GetThirdPlatId()   -- 获取玩家对应的平台id,   类型:uint32
local session  = laccount.GettThirdSession() -- 获取玩家第三方带回的session, 类型:string
local extData  = laccount.GettThirdExtData() -- 获取玩家第三方带回的扩展字段, 类型:string 
local platAccount = laccount.GetThirdPlatAccount() -- 玩家玩家第三方account, 类型:string
local loginIp  = laccount.GetLoginIp()       -- 获取玩家登录ip地址

2. 根据玩家uid获取HTTP通讯的玩家对象

注意
当使用弱联网进行通讯时,注意异步通讯的超时问题。

    local laccountHttp = go.accountmgr.GetAccountById(uid)
    res = {}
    res["do"] = "Cmd.ThirdPlatUserInfo_S"
    res.data = {
        resultCode = 0, 
        uid = uid,
        platId = platId,
        rmb = rmb,
        platPoints = platPoints,
    }
    unilight.success(laccountHttp, res)

3. 根据玩家uid获取TCP通讯的玩家对象

    local laccountTcp = go.roomusermgr.GetRoomUserById(uid)
    res = {}
    res["do"] = "Cmd.ThirdPlatUserInfo_S"
    res.data = {
        resultCode = 0, 
        uid = uid,
        platId = platId,
        rmb = rmb,
        platPoints = platPoints,
    }
    unilight.success(laccountTcp, res)

2. GM流程

前端:platcommon/chatcommand.proto/ GMCommandChatUserPmd_C

实例:{"method":"lottery","params":" p=1","cmd_name":"Pmd.GMCommandChatUserPmd_C"}

后端: 1. GM消息初始化给unilight go.gmcommand.AddLuaCommand(GmCmd.LotteryCtl, "lottery", "//lottery p=2 b=11", true) 一般在服务器起动后,在StartOver()函数进行初始化 2. 定时GM消息

    GmCmd.LotteryCtl = function(laccount, map)
        local lotteryCmnId = tonumber(map["p"])
        local lotteryBssId = tonumber(map["b"])
                // 当有错误时,返回一个字符串
                return "error desc"
                // 当正确执行时
                return nil
    end

3. 强联网主动消息推送

  1. 创建room
    room = go.roommgr.CreateRoom()
  2. 注销room
    lua local id = room.Id go.roommgr.DestroyRoom(id)
  3. 增加account入room
    room.Rum.AddRoomUser(laccount)
  4. Account退出room
    room.Rum.RemoveRoomUser(laccount)
  5. 全员广播
    room.BroadcastString(msg) // msg与普通协议组装一样
  6. 全员广播除了指定uid
    room.BroadcastStringExceptMe(msg, uid)
  7. 强联网玩家掉线通知

    lua Tcp.account_disconnect = function(roomuser) uid = roomuser.Id end 8. unilight主动将一个玩家的长链接断开的接口

    lua local laccount = go.accountmgr.GetAccountById(uid) if laccount ~= nil then laccount.Kickout("描述剔除原因") // 服务器会通知客户端(Pmd.ServerKickoutLoginUserPmd_S)后,将客户端的长链接断开 end

    4. 聊天功能

    聊天unilight中运用go语言已统一实现了对普通聊天的处理功能,只需要客户端发送相应的协议即可

聊天功能涉及主动消息推送,所以需要使用强联网

协议: * platcommon/chatcommand/CommonChatUserPmd_CS * platcommon/chatcommand/PrivateChatUserPmd_CS

5. 定时器

Timer:

以往的定时器为一次addtimer,只触发一次函数后就自动销毁,现在最新Unilight已变为当没有timer.Stop()时,一直在指定间隔内循环执行触发函数
1. 初始化
unilight.addtimer("ChatMgr.CmdRobotChat", 1, “paraTest”)
2. 消息触发

    ChatMgr.CmdRobotChat = function(para, timer)
    end

ps: 触发函数的最后一个参数为这个函数触发的timer对象(go对象),以方便对timer的控制

Timer.Stop()为停止timer

Addclocker:

可参阅gxlua中的unilight.lua说明

6. 支付

现支付可支持第三方平台积分兑换及充值 协议参考:platcommon/skdcommon.proto 后端接收SDKSERVER的Lua回调用法实例:详细见recharge目录的demo
1. 查询平台剩余点数
2. 兑换积分返回
3. 创建订单号返回
4. 代理通知游戏服有玩家充值