-
Notifications
You must be signed in to change notification settings - Fork 204
Http
Bruce edited this page Sep 21, 2022
·
7 revisions
现在游戏开发经常需要对接一些SDK,它们大多使用Http协议交互, Moon提供了简单的支持。
local http_server = require("moon.http.server")
-- http_server.header_max_len = 8192
-- http_server.content_max_len = 8192
http_server.error = function(fd, err)
print("http server fd",fd," disconnected:", err)
end
http_server.on("/get",function(request, response)
print_r(request.parse_query(),"GET")
response:write_header("Content-Type","text/plain")
response:write("GET:Hello World")
end)
http_server.on("/post",function(request, response)
response:write_header("Content-Type","text/plain")
response:write("POST:Hello World/home")
end)
http_server.on("/postform",function(request, response)
print_r(request.parse_form(),"POST_FORM")
response:write_header("Content-Type","text/plain")
response:write("POST_FROM:Hello World/home")
end)
http_server.listen("127.0.0.1",8001)
print("http_server start", "127.0.0.1",8001)moon.async(function ()
httpc.get("127.0.0.1:8001", {
path = "/get?a=1&b=2",
keepalive = 300
})
httpc.post("127.0.0.1:8001","Hello Post", {
path = "/post",
keepalive = 300
})
local form = {username="wang",passwd="456",age=110}
httpc.postform("127.0.0.1:8001", form, {
path = "/postform",
keepalive = 300
})
moon.async(function ()
print_r(httpc.get("www.google.com:443"),{
proxy = "127.0.0.1:10809"
})
end)
end)
Moon对静态文件服务器提供了有限的支持,主要用来做一些简单的后台管理网站,采用进程启动时加载所有静态文件,而不是请求时动态读取。
http_server.static("/www")Moon没有直接提供Https支持,建议使用Nginx代理,Nginx更加简单,专业和高效。
新建 https_forward_proxy.conf 保存到 /etc/nginx/conf.d/ 目录下
server {
listen 8443;
access_log /var/log/nginx/access_proxy-8443.log main;
# dns resolver used by forward proxying
resolver 8.8.8.8;
location / {
proxy_pass https://$http_host$request_uri;
# proxy_set_header Host $host;
proxy_buffers 256 4k;
proxy_max_temp_file_size 0k;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
}
}
---使用
moon.async(function ()
print_r(httpc.get("www.baidu.com:443"),{
proxy = "127.0.0.1:8443"
})
end)新建文件 https_reverse_proxy.conf 保存到 /etc/nginx/conf.d/ 目录下
注意:证书需要自己生成
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name xxxx.com;
root /usr/share/nginx/html;
ssl_certificate "/usr/local/nginx/conf/some_certificate.crt";
ssl_certificate_key "/usr/local/nginx/conf/some_certificate.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location / {
proxy_pass http://127.0.0.1:8001/;
}
}
这样通过访问443端口就是相当于访问http_server的8001端口
http_server.listen("127.0.0.1", 8001)