我认为当我的网络连接中断或恢复时收到通知会很棒,所以我把它放在我的 Hammerspoon init.lua 中:
ping = nil
previousStatus = nil
function pingCallback(server, eventType, ...)
hs.alert.show(eventType)
if eventType == "receivedPacket"
then
newStatus = "success"
else
if eventType == "didFail" or eventType == "sendPacketFailed"
then
newStatus = "failure"
end
end
if not (newStatus == previousStatus)
then
hs.alert.show(string.format("Network status changed to %s", newStatus))
previousStatus = newStatus
end
end
while(true)
do
ping = hs.network.ping.ping("google.com", 5, 1.0, 2.0, "any", pingCallback)
os.execute("sleep 15")
end
问题是睡眠。它让 Hammerspoon 自己睡觉,让它挂起。我真正需要的是一个线程或计时器,或者可能启动一个不同的操作系统进程。我应该怎么办?