mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-23 19:43:09 +00:00
c/s json protocol example
This commit is contained in:
@@ -4,34 +4,19 @@ local db = {}
|
||||
local command = {}
|
||||
|
||||
function command.GET(key)
|
||||
skynet.ret(db[key])
|
||||
return db[key]
|
||||
end
|
||||
|
||||
function command.SET(key, value)
|
||||
local last = db[key]
|
||||
db[key] = value
|
||||
skynet.ret(last)
|
||||
return last
|
||||
end
|
||||
|
||||
local trace_cache = {}
|
||||
|
||||
skynet.trace_callback(function(handle, ti)
|
||||
-- print("TRACE",trace_cache[handle],ti)
|
||||
trace_cache[handle] = nil
|
||||
end)
|
||||
|
||||
skynet.start(function()
|
||||
skynet.dispatch("text", function(session, address, message)
|
||||
trace_cache[skynet.trace()] = message
|
||||
local cmd, key , value = string.match(message, "(%w+) (%w+) ?(.*)")
|
||||
local f = command[cmd]
|
||||
if f then
|
||||
f(key,value)
|
||||
else
|
||||
skynet.ret("Invalid command : "..message)
|
||||
end
|
||||
skynet.dispatch("lua", function(session, address, cmd, ...)
|
||||
local f = command[string.upper(cmd)]
|
||||
skynet.ret(skynet.pack(f(...)))
|
||||
end)
|
||||
skynet.register "SIMPLEDB"
|
||||
end)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user