123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- local skynet = require "skynet"
- local httpc = require "http.httpc"
- local httpurl = require "http.url"
- local dns = require "skynet.dns"
- local runconfig = require "run_config"
- local socket = require "skynet.socket"
- local httpd = require "http.httpd"
- local sockethelper = require "http.sockethelper"
- local cjson = require "cjson"
- local skynet_manager = require "skynet.manager"
- local function http_test(protocol)
- --httpc.dns() -- set dns server
- httpc.timeout = 100 -- set timeout 1 second
- print("GET baidu.com")
- protocol = protocol or "http"
- local respheader = {}
- local host = string.format("%s://baidu.com", protocol)
- print("geting... ".. host)
- local status, body = httpc.get(host, "/", respheader)
- print("[header] =====>")
- for k,v in pairs(respheader) do
- print(k,v)
- end
- print("[body] =====>", status)
- print(body)
- local respheader = {}
- local ip = dns.resolve "baidu.com"
- print(string.format("GET %s (baidu.com)", ip))
- local status, body = httpc.get(host, "/", respheader, { host = "baidu.com" })
- print(status)
- end
- local function response(id, ...)
- local ok, err = httpd.write_response(sockethelper.writefunc(id), ...)
- if not ok then
- -- if err == sockethelper.socket_error , that means socket closed.
- skynet.error(string.format("fd = %d, %s", id, err))
- end
- end
- function json_test()
- local myTable = {name = "John", age = 30, city = "New York"}
- local jsonStr = cjson.encode(myTable)
- print(jsonStr)
- jsonStr = '{"name":"John","age":30,"city":"New York"}'
- local decodedTable = cjson.decode(jsonStr)
- print(decodedTable.name)
- print(decodedTable.age)
- print(decodedTable.city)
- end
- skynet.start(function()
- local srv = skynet.newservice("agentmgr", "agentmgr", 0)
- skynet.name("agentmgr", srv)
- local db_srv = skynet.newservice("dbmgr", "dbmgr", 0)
- skynet.name("dbmgr", db_srv)
- local tools_srv = skynet.newservice("tools_work", "tools_work", 0)
- skynet.name("tools_work", tools_srv)
- local public_srv = skynet.newservice("public_mgr", "public_mgr", 0)
- skynet.name("public_mgr", public_srv)
- local conifg_srv = skynet.newservice("config_mgr", "config_mgr", 0)
- skynet.name("config_mgr", conifg_srv)
- local back_srv = skynet.newservice("backmgr", "backmgr", 0)
- skynet.name("backmgr", back_srv)
- local http_works = {}
- local protocol = "http"
- for i= 1, 30 do --开启30个服务用来接收消息
- http_works[i] = skynet.newservice("http_work", "http_work", protocol)
- end
- local balance = 1
- local id = socket.listen("0.0.0.0",runconfig.testHttpProt )
- -- json_test()
- skynet.error(string.format("Listen web port 8001 protocol:%s", protocol))
- socket.start(id , function(id, addr)
- -- skynet.error(string.format("%s connected, pass it to http_works :%08x", addr, http_works[balance]))
- skynet.send(http_works[balance], "lua", id,addr)
- balance = balance + 1
- if balance > #http_works then
- balance = 1
- end
- end)
- end)
|