How to read/write on the socket address returned by serverstart('127.0.0.1:0')
?
#24637
Replies: 2 comments 1 reply
-
Or I should use uv.tcp_xxx API found in doc: https://neovim.io/doc/user/luvref.html#uv_tcp_t. local tcp_handler = uv.new_tcp()
local uv.tcp_open(tcp_handler, sockaddr) After read the create a TCP server (introduced here: https://neovim.io/doc/user/luvref.html#luv-intro): local uv = vim.loop
local server = uv.new_tcp()
server:bind("127.0.0.1", 0) -- Set the port to 0, so it will bind to an available port
server:listen(128, function (err)
assert(not err, err)
local client = uv.new_tcp()
server:accept(client)
client:read_start(function (err, chunk)
assert(not err, err)
if chunk then
client:write(chunk)
else
client:shutdown()
client:close()
end
end)
end)
print("TCP server listening at 127.0.0.1 port 1337")
uv.run() -- an explicit run call is necessary outside of luvit |
Beta Was this translation helpful? Give feedback.
-
I think I should write something here, I found some source code from fzf-lua, and I think the source code answered my question: When fzf-lua setup, it setup a server via -- Create a new RPC server (tmp socket) to listen to messages (actions/headless)
-- this is safer than using $NVIM_LISTEN_ADDRESS. If the user is using a custom
-- fixed $NVIM_LISTEN_ADDRESS, different neovim instances will use the same path
-- as their address and messages won't be received on older instances
if not vim.g.fzf_lua_server then
vim.g.fzf_lua_server = vim.fn.serverstart()
end Then in somewhere else, it connects this socket server via
local chan_id = vim.fn.sockconnect("pipe", opts.fzf_lua_server, { rpc = true })
vim.rpcrequest(chan_id, "nvim_exec_lua", [[
local luaargs = {...}
local function_id = luaargs[1]
local preview_socket_path = luaargs[2]
local fzf_selection = luaargs[3]
local fzf_lines = luaargs[4]
local fzf_columns = luaargs[5]
local usr_func = require"fzf-lua.shell".get_func(function_id)
return usr_func(preview_socket_path, fzf_selection, fzf_lines, fzf_columns)
]], {
opts.fnc_id,
preview_socket_path,
fzf_selection,
tonumber(preview_lines),
tonumber(preview_cols)
})
vim.fn.chanclose(chan_id) And let's read doc again: https://neovim.io/doc/user/builtin.html#serverstart(), I believe the The last code snippet looks like doing:
This is different from what I had thought, I'm thinking about something like pure TCP client/server. But actually I think can use these APIs because it should also works. |
Beta Was this translation helpful? Give feedback.
-
Hi, I want to setup a tcp server with:
For example here it returns
127.0.0.1:60312
to me.Then how should I bind it to an
accpet
function, to handle the received data?I am using:
Is that correct?
If so, now I want to connect to the server somewhere else (even another process) as a tcp client, I am using:
Is that correct?
Or I must use
chansend
with thelisten_chanid
, without connecting to the socket address again?What if the client is in another process, not inside the nvim editor? Is the chan id still valid across different process or even different OS/machines?
Beta Was this translation helpful? Give feedback.
All reactions