Open k0xxxx opened 3 months ago
It would be easier if websocat could work with two connections at once, but I did not find such information in the documentation
Not possible with current's Websocat1, possible in future's Websocat4 using custom scripts.
Here is example of connecting to wss://ws.vi-server.org/mirror/1
and wss://ws.vi-server.org/mirror/2
simultaneously (with some traffic logging):
cat > script.rh << \EOF
let tlsctx1 = tls_client_connector(#{});
parallel([
lookup_host("ws.vi-server.org:443", |addrs1| {
connect_tcp_race(#{}, addrs1, |tcp1| {
tls_client(#{domain: "ws.vi-server.org"}, tlsctx1, tcp1, |tls1| {
let http1 = http1_client(#{}, tls1);
ws_upgrade(#{url: "/mirror/1/",host: "ws.vi-server.org",}, http1, |wsframes1| {
let ws1 = ws_wrap(#{client: true}, wsframes1);
let log1 = datagram_logger(#{read_prefix: "READ1 ", write_prefix: "WRITE1 "}, ws1);
let stdio1 = create_stdio();
let chunks1 = line_chunks(#{substitute: 32,}, stdio1);
exchange_packets(#{}, log1, chunks1)
})
})
})
}),
lookup_host("ws.vi-server.org:443", |addrs1| {
connect_tcp_race(#{}, addrs1, |tcp1| {
tls_client(#{domain: "ws.vi-server.org"}, tlsctx1, tcp1, |tls1| {
let http1 = http1_client(#{}, tls1);
ws_upgrade(#{url: "/mirror/2/",host: "ws.vi-server.org",}, http1, |wsframes1| {
let ws1 = ws_wrap(#{client: true}, wsframes1);
let log1 = datagram_logger(#{read_prefix: "READ2 ", write_prefix: "WRITE2 "}, ws1);
let stdio1 = create_stdio();
let chunks1 = line_chunks(#{substitute: 32,}, stdio1);
exchange_packets(#{}, log1, chunks1)
})
})
})
}),
])
EOF
websocat4 -x script.rh
Both connections will fight for input from stdin though.
You can get script samples using websocat4 --dump-spec ...
commands.
But just like with your existing solution, "I have concerns that data from site.com/1 and site.com/2 may arrive at the same time." is still a problem and can lead to garbled data.
I'll probably add accurate combinator to Websocat4 later (or maybe sooner, if you are open to alpha test it).
As for exiting, you may put all the processes into process group and terminate the whole process group later.
Thanks for the example, but I'm afraid I don't know the Rust programming language and I may be less useful than you expect. But I'm willing to make an effort to help you with alpha testing if you accompany me during the testing process and provide feedback.
As for exiting, you may put all the processes into process group and terminate the whole process group later.
I tried to do it like this: (echo "test" | websocat -n wss://site.com/1 & websocat wss://site.com/2) | php bot.php
But in my case it didn't work. Please could you explain in more detail?
Here is a Bash command line that force termination of a backgrounded process:
(echo "test" | websocat -n wss://site.com/1 & P=$!; trap "kill $P" EXIT; websocat wss://site.com/2) | php bot.php
Note that if you just need to send a message to other URL (without keeping subscribed for further replies), you may use -1
(--one-message
) option to make Websocat exit after sending the message (or maybe rather receiving a reply) on itself.
Hi Vi! I've been racking my brain trying to find a solution, please help me. I need to pass some text to the websocat utility via "echo" and establish a connection to the first address, then make another connection to the second URL and redirect websocat to redirect the stdout of both processes to
php file.php
. I came up with a solution, but it doesn't work correctly:(echo "test" | websocat -n wss://site.com/1 & websocat wss://site.com/2) | php bot.php
But the problem is that when the "php bot.php" process terminates, thewebsocat wss://site.com/2
process continues to hang in memory (because the server doesn't close the connection), and I need all processes in this expression to terminate. When thephp bot.php
process terminates, I see the following message in the console: Exit: disconnectedExit: disconnected - indicates that the php bot.php process terminated its work using the exit() function;
At the same time, as I already said, the
websocat wss://site.com/2
process continues to hang in memory and continue its execution in the console, which I can only do with CTRL+C or Enter key.All I need is that when the php bot.php process terminates its work, all websocket connections are also closed and websocat processes are also terminated correctly. I noticed that if after the completion of the php process, data from the server comes to the
websocat wss://site.com/2
process via websocket, then websocat terminates its work, albeit with error messages in the console.PS: I am not completely sure about the correctness of the construction
(echo "test" | websocat -n wss://site.com/1 & websocat wss://site.com/2) | php bot.php
I have concerns that data from site.com/1 and site.com/2 may arrive at the same time. For example, site.com/1 sends "Hello", and site.com/2 sends World, and the PHP script may output HeWorldllo. It would be easier if websocat could work with two connections at once, but I did not find such information in the documentation
Perhaps in this case it would be more correct to create a websocat server and connect to it websocat wss://site.com/1, websocat wss://site.com/2 and through another websocat instance directly the php bot.php script itself