sách gpt4 ai đã đi

LuaLanes 和 LuaSockets

In lại 作者:行者123 更新时间:2023-12-04 14:03:15 25 4
mua khóa gpt4 Nike

我正在开发一个使用套接字与外界通信的小 Lua 应用程序(在 Lua for Windows 下,如果这很重要)。 (LuaSocket)

我正在尝试同时发出几个请求。所以我认为 LuaLanes 是要走的路。 (当然,如果有更好的解决方案,我对替代方案持开放态度,但我不想为此处理协程。)

像这样的东西:

server = assert (socket.bind ('*', 1234))
client = server : accept ()
-- set id to some unique value
allClients [id] = client
theLane = lanes.gen ("", laneTest) ( id )
print (theLane [1])

laneTest函数定义如下:
function laneTest (id)
local client = allClients [id]
print ('peer: ', client:getpeername())
kết thúc

我的问题是在 laneTest里面功能,当作为车道运行时,我收到这个可爱的错误消息:

attempt to index local 'client' (a userdata value)



(来自 client:getpeername() 行)

所以..我不确定这里发生了什么?车道与 socket 不兼容,还是我做错了什么?

我想这可能是 Lua for Windows 附带的 channel 版本很古老( luaforwindows )并且不适用于套接字,但最新版本可能吗? (车道 2.0.4 与最近的 3.xx)

我真的不知道如何更新我拥有的 Lanes 版本,否则我现在已经尝试过了,所以。如果那是我要去的地方,或者有更明显的事情我做错了,我将不胜感激。

biên tập:
我继续通过 luarocks 安装了车道,并且使用安装为岩石的车道 3.1.6-1 遇到了同样的问题。

编辑2:
试过这个(仍然失败):
require ('socket')
require ('lanes')
local allClients = {}

function theLane (id)
print ('the id:', id) -- correctly prints out the id passed to the function
local SOCKET = require ('socket')
local client = allClients [id]
print ('peer:', client:getpeername())
client : close ()
kết thúc

local server = assert (SOCKET.bind ('*', 1234))
local ip, port = server:getsockname ()
local laneFunc = lanes.gen('', theLane)
local client = server:accept ()
allClients [1] = client
local x = laneFunc (1)
print (x[1])
  • 这无法声明:attempt to call global 'require' (a nil value)
  • xóa bỏ require ('socket')函数内的行和重试也失败说:attempt to index local 'client' (a userdata value)

  • 我提前为遗漏了明显的东西道歉,但是......你如何让 socket 与车道一起工作?

    编辑3:

    好吧,我正在编辑它以备将来引用:)

    据我所知,如果不修补 luasockets,就无法在 Sockets 中使用 Lanes。见讨论 đây有关更多信息;但简而言之(正如德科的回答中所解释的):车道不适用于用户数据。 luasocket 不提供任何其他访问套接字/套接字信息的方式。

    我不想修补 luasocket,因为我宁愿使用车道,我会继续坚持使用 copas 或 couroutines。

    Cảm ơn mọi người!

    1 Câu trả lời

    编程 Lua 在 non-preemptive multithreading 上有例子(使用协程),您几乎可以直接使用它。在我看来,协程将是您用例的更好解决方案。

    还有copas library它将自己描述为“基于协程的调度程序,可以被 TCP/IP 服务器使用”,但实际上您也可以将它用于异步发送请求(使用 addthreadstep 调用的组合)。

    关于LuaLanes 和 LuaSockets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13219570/

    25 4 0
    行者123
    Hồ sơ cá nhân

    Tôi là một lập trình viên xuất sắc, rất giỏi!

    Nhận phiếu giảm giá Didi Taxi miễn phí
    Mã giảm giá Didi Taxi
    Giấy chứng nhận ICP Bắc Kinh số 000000
    Hợp tác quảng cáo: 1813099741@qq.com 6ren.com