123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- local skynet = require "skynet"
- local socket = require "skynet.socket"
- local logger = require "logger"
- local table_concat = table.concat
- local skynet_send = skynet.send
- local function ipaddr_parse(addr)
- -- check for format 1.11.111.111 for IPV4
- local chunks = {addr:match("(%d+)%.(%d+)%.(%d+)%.(%d+)")}
- if #chunks == 4 then
- return table_concat(chunks, ".")
- end
- -- check for IPV6 format, should be 8 'chunks' of numbers/letters
- -- without trailing chars
- local chunks = {addr:match(("([a-fA-F0-9]*):"):rep(8):gsub(":$","$"))}
- if #chunks == 8 then
- return table_concat(chunks, ":")
- end
- return addr
- end
- skynet.start(function()
- logger.label("<Webserver>")
- local agent = {}
- for index=1, 3 do
- table.insert(agent, skynet.newservice("webagent", index))
- end
- local balance = 1
- local webport = assert(option.web_port)
- local id = socket.listen(option.web_host, webport)
- logger.info(option.web_host ..":%d", webport)
- socket.start(id, function(fd, addr)
- local ipaddr = ipaddr_parse(addr)
- skynet_send(agent[balance], "lua", "dispatch", fd, ipaddr)
- balance = balance + 1
- if balance > #agent then
- balance = 1
- end
- end)
- end)
|