JsSProto.lua 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. local skynet = require "skynet"
  2. local cjson = require "cjson"
  3. local logger = require "logger"
  4. local trace = logger.trace
  5. local cjson_decode = cjson.decode
  6. local cjson_encode = cjson.encode
  7. local Sproto = {}
  8. Sproto.encode = function (name, args, session)
  9. local list = {
  10. type = name,
  11. session = session,
  12. content = args or {}
  13. }
  14. local ret = cjson_encode(list)
  15. trace("======= encode =======:%s", ret)
  16. return ret
  17. end
  18. Sproto.decode = function(msg, sz)
  19. local str = nil
  20. if(sz) then
  21. str = skynet.tostring(msg, sz)
  22. -- skynet.trash(msg,sz)
  23. else
  24. str = msg
  25. end
  26. trace("======= decode =======:%s", str)
  27. local ret = cjson_decode(str)
  28. local list = {}
  29. local function fun(args)
  30. if args then
  31. list.type = ret.type.."_rsp"
  32. list.session = ret.session
  33. list.content = args
  34. end
  35. local temp = cjson_encode(list)
  36. trace("c2s args pace:%s", temp)
  37. return temp
  38. end
  39. -- if ret.session then
  40. return "REQUEST", ret.type, ret.content, fun
  41. -- else
  42. -- return "REQUEST", ret.type, ret.content, nil
  43. -- end
  44. end
  45. return Sproto