mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
add skynet.watch for skynet.call, read http://blog.codingnow.com/2013/12/skynet_monitor.html
This commit is contained in:
@@ -11,6 +11,10 @@ function command.HELLO()
|
||||
skynet.ret(skynet.pack("hello"))
|
||||
end
|
||||
|
||||
function command.EXIT()
|
||||
skynet.exit()
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
skynet.dispatch("lua", function(session,addr, cmd, ...)
|
||||
command[cmd](...)
|
||||
|
||||
@@ -2,13 +2,39 @@ local skynet = require "skynet"
|
||||
|
||||
-- It's a simple service exit monitor, you can do something more when a service exit.
|
||||
|
||||
local service_map = {}
|
||||
|
||||
skynet.register_protocol {
|
||||
name = "client",
|
||||
id = 3,
|
||||
unpack = function() end,
|
||||
dispatch = function(_, address)
|
||||
local w = service_map[address]
|
||||
if w then
|
||||
for watcher in pairs(w) do
|
||||
skynet.send(watcher, "exit", address)
|
||||
end
|
||||
service_map[address] = false
|
||||
end
|
||||
print(string.format("[:%x] exit", address))
|
||||
end
|
||||
}
|
||||
|
||||
skynet.start(function() end)
|
||||
local function monitor(session, watcher, command, service)
|
||||
assert(command, "WATCH")
|
||||
local w = service_map[service]
|
||||
if not w then
|
||||
if w == false then
|
||||
skynet.ret(skynet.pack(false))
|
||||
return
|
||||
end
|
||||
w = {}
|
||||
service_map[service] = w
|
||||
end
|
||||
w[watcher] = true
|
||||
skynet.ret(skynet.pack(true))
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
skynet.dispatch("lua", monitor)
|
||||
end)
|
||||
|
||||
9
service/testping.lua
Normal file
9
service/testping.lua
Normal file
@@ -0,0 +1,9 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
skynet.start(function()
|
||||
local ps = skynet.uniqueservice("pingserver")
|
||||
skynet.watch(ps)
|
||||
print(skynet.call(ps, "lua", "PING", "hello"))
|
||||
skynet.send(ps, "lua", "EXIT")
|
||||
print(skynet.call(ps, "lua", "PING", "hay"))
|
||||
end)
|
||||
Reference in New Issue
Block a user