该库旨在演示 rolua 与 golang 的无缝集成
// rohttp-server.go
package rolib
import (
"net/http"
"github.com/arnodel/golua/lib/golib"
rt "github.com/arnodel/golua/runtime"
)
func Init() *rt.Table {
pkg := rt.NewTable()
pkg.Set(
rt.StringValue("NewServeMux"),
rt.FunctionValue(rt.NewGoFunction(NewServeMux, "NewServeMux", 0, false)),
)
pkg.Set(
rt.StringValue("ListenAndServe"),
rt.FunctionValue(rt.NewGoFunction(ListenAndServe, "ListenAndServe", 2, false)),
)
return pkg
}
func NewServeMux(t *rt.Thread, gc *rt.GoCont) (rt.Cont, error) {
return gc.PushingNext(
t.Runtime,
golib.NewGoValue(
t.Runtime,
http.NewServeMux(),
),
), nil
}
func ListenAndServe(t *rt.Thread, gc *rt.GoCont) (rt.Cont, error) {
if err := gc.CheckNArgs(2); err != nil {
return nil, err
}
addr := gc.Arg(0).AsString()
handler := gc.Arg(1).AsUserData().Value().(*http.ServeMux)
http.ListenAndServe(addr, handler)
return gc.Next(), nil
}
lua 端调用
local rolib = require("rolib")
local s = rolib.require("rohttp-server.go")
local mux = s.NewServeMux()
mux.HandleFunc("/", function(w, r)
w.Write('hi there from Lua! You requested ' .. r.URL.Path .. '\n')
end)
s.ListenAndServe(":10907", mux)
基于 go 的 net/http,将其导出给 lua 使用
加入 rolua 交流群
访问链接加入云湖群聊【RoLua 交流群】
https://yhfx.jwznb.com/share?key=WQ0t9mLaWrbc&ts=1768570782
群ID: 490767726
RoLua 是一个纯 Golang 写的 Lua5.4 编译器/运行时,在 arnodel/golua 的基础上增加了许多功能
项目地址:https://github.com/runoneall/rolua
1
1


快银 VPN,一键连接全球节点,线路稳定、延迟低。支持 Windows、macOS 等多平台,TUN 全局模式覆盖全部应用流量。无需复杂配置,下载即用。
1 条回复
排序