message.lua 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. --[[
  2. {
  3. action = "xxx",
  4. method = "xxx",
  5. status = "success" / "failed"
  6. data = ...
  7. }
  8. ]]
  9. local Message = class("Message")
  10. local cjson = require( "cjson" )
  11. function Message:ctor(action, data, ex1, ex2, ex3)
  12. self.action = action
  13. self.data = data
  14. -- 增加扩展参数,只在发送时使用,服务器做不到解析json给脚本
  15. self.ex1 = ex1
  16. self.ex2 = ex2
  17. self.ex3 = ex3
  18. end
  19. function Message:load(stream)
  20. local obj = cjson.decode(stream)
  21. if not obj then
  22. return nil
  23. end
  24. self:setAction(obj.action)
  25. self:setData(obj.data)
  26. return self
  27. end
  28. function Message:dump()
  29. local obj = {
  30. action = self:getAction(),
  31. data = self:getData(),
  32. ex1 = self.ex1,
  33. ex2 = self.ex2,
  34. ex3 = self.ex3,
  35. }
  36. return cjson.encode(obj)
  37. end
  38. function Message:setAction(action)
  39. self.action = action
  40. end
  41. function Message:getAction()
  42. return self.action
  43. end
  44. function Message:setData(data)
  45. self.data = data
  46. end
  47. function Message:getData()
  48. return self.data
  49. end
  50. return Message