Linux command
websocat 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Connect to a WebSocket server
websocat ws://[example.com]/socket
Connect with secure WebSocket
websocat wss://[example.com]/socket
Create a WebSocket server
websocat -s [8080]
Pipe stdin to WebSocket
echo "hello" | websocat ws://[example.com]/socket
Connect with custom headers
websocat -H "Authorization: Bearer [token]" wss://[example.com]/socket
Binary mode
websocat -b ws://[example.com]/socket
One message per line
websocat -n ws://[example.com]/socket
Forward TCP to WebSocket
websocat --binary tcp-l:127.0.0.1:[8080] ws://[example.com]/socket
说明
websocat is a command-line WebSocket client and server, similar to netcat but for WebSocket connections. It enables bidirectional communication between various endpoints including WebSocket, TCP, Unix sockets, and stdio. Basic usage connects stdin/stdout to a WebSocket URL. Input lines become WebSocket messages; received messages print to stdout. Use -b for binary data or -n for line-based text. The server mode (-s) creates a simple WebSocket server useful for testing. For production, combine address specifiers like tcp-l: with ws:// for protocol conversion. Address chaining enables complex setups like WebSocket tunnels, TCP-over-WebSocket, and command execution. The autoreconnect: wrapper handles connection drops automatically. websocat supports both secure (wss://) and insecure (ws://) connections, custom headers for authentication, and ping/pong for connection keepalive.
参数
- -s, --server
- Simple WebSocket server mode on specified port.
- -b, --binary
- Send and receive binary frames instead of text.
- -n, --no-close
- Don't send close frame, disconnect abruptly.
- -1, --one-message
- Send one message and exit.
- -u, --unidirectional
- Only copy from address1 to address2.
- -U, --unidirectional-reverse
- Only copy from address2 to address1.
- -H _header_
- Add custom HTTP header.
- -E, --exit-on-eof
- Exit when stdin reaches EOF.
- -t, --text
- Force text frame mode.
- --ping-interval _secs_
- Send WebSocket pings at interval.
- --ping-timeout _secs_
- Close connection if pong not received.
- -k, --insecure
- Accept invalid TLS certificates.
- --origin _url_
- Set Origin header.
- -q, --quiet
- Suppress informational messages.
- -v, --verbose
- Increase verbosity level.
FAQ
What is the websocat command used for?
websocat is a command-line WebSocket client and server, similar to netcat but for WebSocket connections. It enables bidirectional communication between various endpoints including WebSocket, TCP, Unix sockets, and stdio. Basic usage connects stdin/stdout to a WebSocket URL. Input lines become WebSocket messages; received messages print to stdout. Use -b for binary data or -n for line-based text. The server mode (-s) creates a simple WebSocket server useful for testing. For production, combine address specifiers like tcp-l: with ws:// for protocol conversion. Address chaining enables complex setups like WebSocket tunnels, TCP-over-WebSocket, and command execution. The autoreconnect: wrapper handles connection drops automatically. websocat supports both secure (wss://) and insecure (ws://) connections, custom headers for authentication, and ping/pong for connection keepalive.
How do I run a basic websocat example?
Run `websocat ws://[example.com]/socket` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -s, --server do in websocat?
Simple WebSocket server mode on specified port.