123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 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()
- skynet.uniqueservice("dbproxy", "book_server")
- skynet.uniqueservice("agent_manager", "agent_manager")
- local back_srv = skynet.newservice("backmgr", "backmgr", 0)
- skynet.name("backmgr", back_srv)
- --接收消息的服务
- local httpworks = {}
- local protocol = "http"
- for i= 1, 30 do --开启30个服务用来接收消息
- httpworks[i] = skynet.newservice("http_work", "http_work", protocol)
- end
- local http_balance = 1
- local id = socket.listen("0.0.0.0",runconfig.httpProt )
- socket.start(id , function(id, addr)
- skynet.send(httpworks[http_balance], "lua", id,addr)
- http_balance = http_balance + 1
- if http_balance > #httpworks then
- http_balance = 1
- end
- end)
- end)
|