This Bash script establishes a WebSocket connection to a server, allowing users to send a sequence of messages interactively. It is particularly useful for testing WebSocket APIs or automating WebSocket-based data subscriptions.
The script leverages websocat for WebSocket communication. If websocat is not installed, the script will prompt the user to install it automatically.
- Interactive Setup: The script prompts the user for essential parameters, including:
- WebSocket URL: Defaults to wss://example.com if left blank.
- Username and Password: For authentication in the WebSocket server.
- Currency (CCY): Used to specify the currency in the subscription message.
- Customizable Sleep Time: Allows a delay between messages, with the first message having a fixed 1-second delay and subsequent messages using a user-defined interval.
- Automatic websocat Installation: If websocat is not installed, the script prompts the user to install it. Upon confirmation, it downloads the latest version of websocat from GitHub, makes it executable, and moves it to /usr/local/bin/ for system-wide access.
- Sequential Message Sending: Sends messages in a specified sequence, waiting for a server response after each message.
- Response Capture: Reads and displays server responses in real-time, allowing the user to observe responses to each message.
- Run the Script:
chmod +x ./websocket_script.sh
./websocket_script.sh
- Follow the Prompts:
- Enter the WebSocket URL or press Enter to use the default URL.
- Provide your username, password, and currency for the subscription.
- Set a custom sleep time for subsequent messages, or press Enter to default to 1 second.
- Observe Responses:
- The script sends each message in sequence, starting with a login request and followed by a subscription request for the specified currency.
- Each server response is displayed in real-time as "Received: "