Skip to content

Instantly share code, notes, and snippets.

@KamranBiglari
Last active November 7, 2024 11:50
Show Gist options
  • Select an option

  • Save KamranBiglari/588f13900d77c80e61405b0bb9529b74 to your computer and use it in GitHub Desktop.

Select an option

Save KamranBiglari/588f13900d77c80e61405b0bb9529b74 to your computer and use it in GitHub Desktop.
WebSocket Client Script with Dynamic User Prompts and Auto Installation of websocat

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.

Features

  • 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.

Example Usage

  1. Run the Script:
chmod +x ./websocket_script.sh
./websocket_script.sh
  1. 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.
  1. 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: "
#!/bin/bash
# Check if websocat is installed
if ! command -v websocat &> /dev/null; then
read -p "websocat is not installed. Would you like to install it now? (y/n): " install_websocat
if [[ "$install_websocat" == "y" || "$install_websocat" == "Y" ]]; then
# Attempt to download and install websocat
echo "Installing websocat..."
wget -q https://github.com/vi/websocat/releases/latest/download/websocat.x86_64-linux -O websocat
chmod +x websocat
sudo mv websocat /usr/local/bin/
# Verify if installation was successful
if command -v websocat &> /dev/null; then
echo "websocat installed successfully."
else
echo "Failed to install websocat. Please install it manually from https://github.com/vi/websocat."
exit 1
fi
else
echo "websocat is required to run this script. Exiting."
exit 1
fi
fi
# Prompt for WebSocket URL with a default value
read -p "Enter WebSocket URL (default is wss://example.com): " WEBSOCKET_URL
WEBSOCKET_URL=${WEBSOCKET_URL:-wss://example.com}
# Prompt for USERNAME, PASSWORD, CCY, and optional SLEEP time
read -p "Enter Username: " USERNAME
read -s -p "Enter Password: " PASSWORD
echo
read -p "Enter Currency (CCY): " CCY
read -p "Enter sleep time for subsequent messages (default is 1 second): " SLEEP
# Use default sleep time of 1 second if the user leaves input blank
SLEEP=${SLEEP:-1}
# Array of messages to send in sequence with user-provided values
messages=(
"{\"request\": \"login\", \"username\": \"$USERNAME\", \"password\": \"$PASSWORD\"}"
"{\"request\": \"subscribe\", \"ccy\": \"$CCY\"}"
)
# Start websocat in interactive mode, and pipe commands to it
{
# Sleep for 2 second before sending the first message to allow websocket connection established
sleep 2
for i in "${!messages[@]}"; do
# Send each message
echo "${messages[$i]}"
# Use a 1-second sleep for the first message, and user-defined sleep for others
if [ "$i" -eq 0 ]; then
sleep 1
else
sleep "$SLEEP"
fi
done
} | websocat "$WEBSOCKET_URL" | while IFS= read -r response; do
echo "Received: $response"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment