[英]How to check if a nonblocking luasocket client has lost it's connection?
我正在編寫用於OBS的Lua腳本,該腳本通過TCP連接不斷從ProPresenter(另一個程序)接收數據。 我使用LuaSocket庫進行連接,並且按預期方式獲取數據。
問題是,當我關閉ProPresenter時,無法將腳本注冊為關閉連接,而我將luasocket超時設置為0(因為它是無阻塞連接)。 我需要腳本始終處於非阻塞狀態,否則它將導致所有OBS停頓並且幀速率降至1以下...
但是,如果我將超時設置為例如。 1秒鍾,luasocket記錄連接已無故障關閉,根據此示例,當超時為0時,它也應正常工作。 但是顯然不是,我懷疑這是因為該示例使用了較舊的Luasocket版本,並且最新版本可能已更改。
這是我的代碼:
不注冊連接已關閉對超時的二重奏:
function recv_and_process_data()
local data
data, err, partial = s:receive()
if data ~= nil then
--process the recieved data. This part works.
elseif err == "closed" then
--doesn't get here because of timeout...
--inform script that the connection has closed
elseif err == "timeout" then
--goes here as soon as ProPresenter is closed
print(err .. " partial: " .. partial)
end
end
連接關閉時注冊,但使OBS停頓:
function recv_and_process_data()
local data
s:settimeout(1) --timeout set to 1 second
data, err, partial = s:receive()
s:settimeout(0)
if data ~= nil then
--process the recieved data. This part works.
elseif err == "closed" then
--goes here when ProPresenter is closed
--inform script that the connection has closed
elseif err == "timeout" then
print(err .. " partial: " .. partial)
end
end
這也不起作用(如這里的建議):
function recv_and_process_data()
local data
data, err, partial = s:receive(0)
if err == "closed" then
print(err .. " partial: " .. partial)
end
end
如果我無法正常工作,我想我必須嘗試重新連接以查看ProPresenters服務器是否仍在運行。
我也試圖找出這個問題。 我發現很小的settimeout()值仍然會返回一個錯誤,您可以使用該錯誤,但根本無法阻止該程序。
我使用local Data, Error = Client:settimeout(0.0001)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.