#!/bin/bash # Send a message on the group via WebSocket (requires websocat) source "$(dirname "$0")/config.sh" TOKEN1=$(load_state "TOKEN1") GROUP_ID=$(load_state "GROUP_ID") echo "=== Debug: loaded state ===" echo " TOKEN1: ${TOKEN1:0:20}..." echo " GROUP_ID: $GROUP_ID" if [[ -z "$TOKEN1" || -z "$GROUP_ID" ]]; then echo "ERROR: Missing state. Run previous scripts first." exit 1 fi if ! command -v websocat &>/dev/null; then echo "ERROR: websocat is not installed." echo "Install it with: cargo install websocat" echo " or: pacman -S websocat" exit 1 fi AUTH_MSG="{\"token\":\"$TOKEN1\"}" CHAT_MSG="{\"subject\":$GROUP_ID,\"content\":\"Hello from user1!\"}" echo "" echo "=== Sending message to group $GROUP_ID as user1 ===" echo " Auth payload: $AUTH_MSG" echo " Chat payload: $CHAT_MSG" echo "" echo "[*] Connecting to ws://localhost:8080/ws ..." # Use a temp file to capture all ws output WS_OUT=$(mktemp) trap "rm -f $WS_OUT" EXIT ( echo "[>] Sending auth message..." >&2 echo "$AUTH_MSG" sleep 2 echo "[>] Auth sent. Sending chat message..." >&2 echo "$CHAT_MSG" sleep 2 echo "[>] Chat message sent. Closing..." >&2 ) | websocat -v ws://localhost:8080/ws 2>&1 | tee "$WS_OUT" & WS_PID=$! # Wait for websocat to finish or timeout after 10s WAITED=0 while kill -0 $WS_PID 2>/dev/null && [[ $WAITED -lt 10 ]]; do sleep 1 WAITED=$((WAITED + 1)) done if kill -0 $WS_PID 2>/dev/null; then echo "[!] websocat still running after ${WAITED}s, killing..." kill $WS_PID 2>/dev/null wait $WS_PID 2>/dev/null fi echo "" echo "=== WebSocket output ===" cat "$WS_OUT" echo "" echo "=== Done ==="