unpack = function(msg, sz) str1 = skynet.tostring(msg, sz) local data = protobuf.decode("hello.UserInfo", str1) skynet.error("id:"..data.id..";name:"..data.name) -- return host:dispatch(msg, sz) return data.id
encode
1 2 3 4 5 6 7 8 9 10
skynet.fork(function() whiletruedo send_package( protobuf.encode("hello.UserInfo", { id = 233, name = "Hello this is Skynet" }) ) skynet.sleep(500) end end)
local skynet = require"skynet" local socket = require"skynet.socket" local sproto = require"sproto" -- local sprotoloader = require "sprotoloader" local protobuf = require"protobuf"
local WATCHDOG local host local send_request
local CMD = {} local client_fd
localfunctionsend_package(pack) localpackage = string.pack(">s2", pack) socket.write(client_fd, package) end
skynet.register_protocol { name = "client", id = skynet.PTYPE_CLIENT, unpack = function(msg, sz) str1 = skynet.tostring(msg, sz) local data = protobuf.decode("hello.UserInfo", str1) skynet.error("id:"..data.id..";name:"..data.name) return"return:id:"..data.id..";name:"..data.name end, dispatch = function(fd, _, type, ...) -- place game logic here. end }
functionCMD.start(conf) local fd = conf.client local gate = conf.gate WATCHDOG = conf.watchdog protobuf.register_file "./protos/gen/test.pb" skynet.fork(function() whiletruedo send_package( protobuf.encode("hello.UserInfo", { id = 233, name = "Hello this is Skynet" }) ) skynet.sleep(500) end end)
client_fd = fd skynet.call(gate, "lua", "forward", fd) end
functionCMD.disconnect() -- todo: do something before exit skynet.exit() end
skynet.start(function() skynet.dispatch("lua", function(_,_, command, ...) skynet.trace() local f = CMD[command] skynet.ret(skynet.pack(f(...))) end) end)