一.ssrmain登陆游戏注册事件
ssrMessage = require("ssr/ssrgame/net/Message"):Register()  --注册所有的消息处理方法
二.message模块中,分发对应的消息处理方法
local cjson = require("cjson")
local ssrMessage = {}
local dispatch_handler = {}
--分发消息处理的方法
local function _dispatchex(msgID, arg1, arg2, arg3, msgData)
    local msgName = ssrNetMsgCfg[msgID]  --获取到消息的名称 Global_ClickNpcResponse
    if not msgName then
        ssrPrint("error: msgID:"..msgID.." not register!")
        return 
    end
    -- assert(msgName, "msgID:"..msgID.." not register!")
    
    local module, method = msgName:match "([^.]*)_(.*)"  --正则匹配取到模块名字和方法 Global_ClickNpcResponse 
    --modele=Global method=ClickNpcResponse
    --dispatch_handler["Global"] = self
    local target = dispatch_handler[module]  --从handler中获取处理方法表
    if not target or not target[method] then return end
    --self["ClickNpcResponse"] 调用的是 DataPlayerObj:ClickNpcResponse(npcid)
    target[method](target, arg1, arg2, arg3, msgData)  --消息对应处理的 方法 执行 ClickNpcResponse  
   
end
local function _dispatch(msgID, arg1, arg2, arg3, jsonstr)
    local msgData = jsonstr and cjson.decode(jsonstr) or nil   --转成json格式
    
    if ssrNetMsgCfg.sync == msgID then                  --一次性同步登录数据
        for _,v in ipairs(msgData) do
            local id,arg1,arg2,arg3,data = v[1], v[2] or 0, v[3] or 0, v[4] or 0, v[5] or {}
            local jsonstr = cjson.encode(data)
            ssrPrint("dispatch net data: msgID="..id.."  data=", arg1, arg2, arg3, jsonstr)
            local result, errinfo = pcall(_dispatchex, id, arg1, arg2, arg3, data)
            if not result then
                local msgName = ssrNetMsgCfg[id]
                if ssr.isMobile then
                    buglyReportLuaException("msgID="..id .."|msgName="..msgName, errinfo, debug.traceback())
                else
                    ssr.print("LUA ERROR: msgID="..id .."|msgName="..msgName, errinfo, debug.traceback())
                end  
            end
        end
    else
        ssrPrint("dispatch net data: msgID="..msgID.."  data=", arg1, arg2, arg3, jsonstr)
        _dispatchex(msgID, arg1, arg2, arg3, msgData)  --第二次分配消息处理方法给
    end
end
--服务端发送数据
function ssrMessage:sendmsg(msgID, arg1, arg2, arg3, msgData)
    assert(msgID, "ssr sendmsg msgID is nil!")
    if msgData then msgData = cjson.encode(msgData) end
    ssrPrint("send net msgData:", ssrNetMsgCfg[msgID], msgID, arg1, arg2, arg3, msgData)
    ssr.NetworkUtil:SendLuaMsg(msgID, arg1, arg2, arg3, msgData)  --发送消息
end
--传入消息类型,注册处理的方法
--ssrMessage:RegisterNetMsg(ssrNetMsgCfg.Global, self)
--msgType:"Global"
function ssrMessage:RegisterNetMsg(msgType, target)
    if dispatch_handler[msgType] then
        local objname = target.NAME or ""
        --ssr\ssrgame\ui\normalui\dieObj.lua
        ssrPrint("网络消息类【"..msgType.."】已被【"..objname.."】注册")
        return
    end
    --dispatch_handler["Global"] = self
    dispatch_handler[msgType] = target
    
end
function ssrMessage:Register()
    dispatch_handler = {} --定义个保存分发的table
    for msgID,msgName in pairs(ssrNetMsgCfg) do
        if type(msgID) == "number" and (not string.find(msgName, "Request")) then  --是消息ID和不是发送请求的
            --给所有的消息注册处理方法 实现接收服务端发送的消息,并分配_dispatch处理
            ssr.NetworkUtil:RegisterLuaNetworkHandler(msgID, _dispatch)  
        end
    end
    return ssrMessage   --返回消息处理对象
end
return ssrMessage