console.lua 677 B

123456789101112131415161718192021222324252627282930
  1. local skynet = require "skynet"
  2. local snax = require "skynet.snax"
  3. local socket = require "skynet.socket"
  4. local function split_cmdline(cmdline)
  5. local split = {}
  6. for i in string.gmatch(cmdline, "%S+") do
  7. table.insert(split,i)
  8. end
  9. return split
  10. end
  11. local function console_main_loop()
  12. local stdin = socket.stdin()
  13. while true do
  14. local cmdline = socket.readline(stdin, "\n")
  15. local split = split_cmdline(cmdline)
  16. local command = split[1]
  17. if command == "snax" then
  18. pcall(snax.newservice, select(2, table.unpack(split)))
  19. elseif cmdline ~= "" then
  20. pcall(skynet.newservice, cmdline)
  21. end
  22. end
  23. end
  24. skynet.start(function()
  25. skynet.fork(console_main_loop)
  26. end)