HomeServer/examples/webserver.lua
2024-11-20 15:41:37 +08:00

44 lines
1.3 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

local skynet = require "skynet"
local socket = require "skynet.socket"
local httpd = require "http.httpd"
local urllib = require "http.url"
local sockethelper = require "http.sockethelper"
-- 发送html
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
local function handle(id)
socket.start(id) -- 这里一定要start否则不会接收来自客户端的请求
print("handle",id)
local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id), 8192)
print(code, url, method, header, body)
if code then
print(code, url, method, header, body)
local f = io.open("/home/gdut17/skynet_test/websocket/index3.html", "r")
local index_html = f:read("*a")
f:close()
response(id, code, index_html)
end
socket.close(id)
end
local function accept(clientfd, addr)
--skynet.newservice("agent", clientfd, addr)
-- skynet.error("http req ",clientfd,addr)
--handle(clientfd)
skynet.fork(handle,clientfd)
end
skynet.start(function ()
local listenfd = socket.listen("0.0.0.0", 9999)
skynet.error("http server listen on 0.0.0.0:9999")
socket.start(listenfd, accept)
end)