Skynet 通过内部注册的协议进行消息传输。看以下简单例子:
1、首先我们新建一个服务network;
2、然后对其进行发送一条lua消息,让其调用start函数进行输出;
main.lua
local skynet = require "skynet"
local function main()
local n = skynet.newservice "network"
skynet.send(n,"lua","start")
skynet.exit()
end
skynet.start(main)
network.lua
local skynet = require "skynet"
local CMD = { }
function CMD.start(...)
print "hello world!"
end
skynet.start(function()
skynet.dispatch("lua",function(session,type,cmd,...)
local f = assert(CMD[cmd])
if f then
f()
end
end)
end)