Skip to content
Bruce edited this page Jun 24, 2024 · 6 revisions

Http

现在游戏开发经常需要对接一些SDK,它们大多使用Http协议交互, Moon提供了简单的支持。

HttpServer

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("/test_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("/test_post",function(request, response)
    response:write_header("Content-Type","text/plain")
    response:write("POST:Hello World/home")
end)

http_server.on("/test_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)

HttpClient

moon.async(function ()
    local response = httpc.get("127.0.0.1:8001", {
        path = "/test_get?a=1&b=2",
        keepalive = 300
    })

    local response = httpc.post("127.0.0.1:8001","Hello Post", {
        path = "/test_post",
        keepalive = 300
    })

    local form = {username="wang",passwd="456",age=110}
    local response = httpc.postform("127.0.0.1:8001", form, {
        path = "/test_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")

Https

Moon没有直接提供Https支持,建议使用Nginx代理,Nginx更加简单,专业和高效。

HttpClient httphttps正向代理配置

新建 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)
--httpsGoogle登录令牌认证
local function GoogleTokenVerify(token, userId)
    
    local url = string.format("https://oauth2.googleapis.com")
    local response = httpc.get(url, {
        path = string.format("/tokeninfo?id_token=%s", token),
        proxy = "127.0.0.1:8443"
    })
    if response == nil or response.status_code ~= 200 or response.content == nil then 
        return 1
    end

    local jsonStr = json.decode(response.content)
    if jsonStr == nil then 
        return 2
    end

    if jsonStr["sub"] == nil or jsonStr["sub"] ~= userId then 
        return 3
    end

    return 0
end

HttpServer httpshttp反向代理

新建文件 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)