mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
c/s json protocol example
This commit is contained in:
@@ -1,29 +1,36 @@
|
||||
local skynet = require "skynet"
|
||||
local client
|
||||
local jsonpack = require "jsonpack"
|
||||
local netpack = require "netpack"
|
||||
local socket = require "socket"
|
||||
|
||||
local CMD = {}
|
||||
|
||||
local client_fd
|
||||
|
||||
local function send_client(v)
|
||||
socket.write(client_fd, netpack.pack(jsonpack.pack(0,v)))
|
||||
end
|
||||
|
||||
local function response_client(session,v)
|
||||
socket.write(client_fd, netpack.pack(jsonpack.response(session,v)))
|
||||
end
|
||||
|
||||
skynet.register_protocol {
|
||||
name = "client",
|
||||
id = skynet.PTYPE_CLIENT,
|
||||
pack = function(...) return ... end,
|
||||
unpack = skynet.tostring,
|
||||
dispatch = function (session, address, text)
|
||||
-- It's client, there is no session
|
||||
local result = skynet.call("SIMPLEDB", "text", text)
|
||||
skynet.ret(result)
|
||||
unpack = function (msg, sz)
|
||||
return jsonpack.unpack(skynet.tostring(msg,sz))
|
||||
end,
|
||||
dispatch = function (_, _, session, args)
|
||||
local result = skynet.call("SIMPLEDB", "lua", table.unpack(args))
|
||||
response_client(session, result)
|
||||
end
|
||||
}
|
||||
|
||||
function CMD.start(gate , fd)
|
||||
client = skynet.launch("client", fd)
|
||||
skynet.call(gate, "lua", "forward", fd, client)
|
||||
skynet.send(client,"text","Welcome to skynet")
|
||||
end
|
||||
|
||||
function CMD.exit()
|
||||
skynet.kill(client)
|
||||
client = nil
|
||||
client_fd = fd
|
||||
skynet.call(gate, "lua", "forward", fd)
|
||||
send_client "Welcome to skynet"
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
|
||||
@@ -19,16 +19,31 @@ local function dispatch()
|
||||
break
|
||||
end
|
||||
for _, v in ipairs(result) do
|
||||
print(v)
|
||||
local session,t,str = string.match(v, "(%d+)(.)(.*)")
|
||||
assert(t == '-' or t == '+')
|
||||
session = tonumber(session)
|
||||
local result = cjson.decode(str)
|
||||
print("Response:",session, result)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local session = 0
|
||||
|
||||
local function send_request(v)
|
||||
session = session + 1
|
||||
local str = string.format("%d+%s",session, cjson.encode(v))
|
||||
socket.send(fd, str)
|
||||
print("Request:", session)
|
||||
end
|
||||
|
||||
while true do
|
||||
dispatch()
|
||||
local cmd = socket.readline()
|
||||
if cmd then
|
||||
socket.send(fd, cmd)
|
||||
local args = {}
|
||||
string.gsub(cmd, '[^ ]+', function(v) table.insert(args, v) end )
|
||||
send_request(args)
|
||||
else
|
||||
socket.usleep(100)
|
||||
end
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
local skynet = require "skynet"
|
||||
local netpack = require "netpack"
|
||||
|
||||
local CMD = {}
|
||||
local SOCKET = {}
|
||||
@@ -13,7 +14,6 @@ end
|
||||
local function close_agent(fd)
|
||||
local a = agent[fd]
|
||||
if a then
|
||||
skynet.call(a, "lua", "exit")
|
||||
skynet.kill(a)
|
||||
agent[fd] = nil
|
||||
end
|
||||
@@ -29,6 +29,9 @@ function SOCKET.error(fd, msg)
|
||||
close_agent(fd)
|
||||
end
|
||||
|
||||
function SOCKET.data(fd, msg)
|
||||
end
|
||||
|
||||
function CMD.start(conf)
|
||||
skynet.call(gate, "lua", "open" , conf)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user