MPV: Lua socket (non-blocking)
Posted: Wed Jan 31, 2024 11:43 am
Here's a rough example for a non-blocking IP (TCP) port listening for incoming connections, without blocking the video playback:
(OS is Xubuntu 20.04, MPV v0.32.0)
Known issues:
- Timer continues when trying to quit/exit MPV. Must be killed by Ctrl+C.
(OS is Xubuntu 20.04, MPV v0.32.0)
Code: Select all
-- Import LuaSocket library
local socket = require("socket")
print("Socket.")
-- Create a TCP socket and bind it to the localhost, on port 12345
local server = assert(socket.bind("127.0.0.1", 12345))
server:settimeout(2);
print("Server.")
function wait_socket()
print("listening...!")
-- Start listening for connections
local client = server:accept()
if client == nil then
print("nix")
else
-- Handle the client connection
client:send("Hello from MPV!\n")
client:close()
end
end
mp.add_periodic_timer(1, wait_socket)
- Timer continues when trying to quit/exit MPV. Must be killed by Ctrl+C.