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("") 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)