notice.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. local skynet = require "skynet"
  2. require "skynet.manager"
  3. local logger = require "logger"
  4. local stringify = require "stringify"
  5. local usercenter = skynet.localname(".usercenter")
  6. local chat = skynet.localname(".chat")
  7. local cjson = require "cjson"
  8. local md5 = require "md5"
  9. local authz = {acc = "yytx", pwd = "lee@YY-Games.520"}
  10. -- Generate request data
  11. local content = {acc = authz.acc, pwd = authz.pwd, sign = false}
  12. content.sign = string.sub(md5.sumhexa(content.acc .. content.pwd), 9, 24)
  13. local whitelist = {
  14. ["192.168.1.23"] = true,
  15. ["192.168.1.127"] = true,
  16. ["192.168.1.41"] = true,
  17. ["14.29.136.211"] = true,
  18. ["222.212.88.4"] = true,
  19. }
  20. --请求方式: http://服务器ip地址:端口号/notice?code=xxx&ntype=1&interval=10&content=想要发送的跑马灯内容
  21. --示例: http://192.168.1.102:8002/notice?code=xxx&ntype=1&interval=10&content=this%20is%20a%20test
  22. local function notice(args, ipaddr, header)
  23. logger.trace("处理来自主机 %s 的公告请求", ipaddr)
  24. if not whitelist[ipaddr] then
  25. -- return { "403 - Forbidden" }
  26. -- return cjson.encode({errno = 403, host = header.host, info = "不信任ip"})
  27. end
  28. -- 验证gm账号
  29. local code = string.sub(args.code, 1, 16)
  30. if code ~= content.sign then
  31. return cjson.encode({state = 403, msg = "账号或密码错误"})
  32. end
  33. local ntype = math.tointeger(args.ntype) or 1
  34. if ntype < 1 then
  35. return cjson.encode({state = 400, msg = "ntype次数少于 1"})
  36. end
  37. local details = args.content
  38. if not details or #details == 0 then
  39. return cjson.encode({state = 400, msg = "content内容异常"})
  40. end
  41. logger.error("______GM请求发送公告:%s", details)
  42. local interval = math.tointeger(args.interval) or 0
  43. if ntype > 1 then
  44. if interval < 1 then
  45. return cjson.encode({state = 400, msg = "间隔时间太短"})
  46. end
  47. else
  48. interval = 0
  49. end
  50. skynet.fork(function()
  51. for i = 1, ntype do
  52. logger.trace("notice: %s,times:%s,interval:%s", details, ntype,interval)
  53. local now = os.time()
  54. local message = {
  55. h_type = 2, -- 聊天的主类型
  56. c_type = CHAT_SON_TYPE.GM_NOTICE, -- 子类型
  57. content = details, -- 内容
  58. tm = now, -- 时间
  59. sid = option.sid -- 服务器
  60. }
  61. skynet.call(chat, "lua", "reqChat", message)
  62. -- skynet.call(usercenter, "lua", "broadcast", "notice", content)
  63. skynet.sleep(interval * 100)
  64. end
  65. end)
  66. logger.warn("notice (%s, %s)", code, ipaddr)
  67. return cjson.encode({state = 0, msg = "success"})
  68. end
  69. return notice