Created
November 12, 2021 21:13
-
-
Save kevinswiber/a0fd2d9e8b327d6b8f9a5a7bfa097085 to your computer and use it in GitHub Desktop.
A simple bash script for checking if a WebSocket endpoint is alive.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
self=`basename "$0"` | |
if (($# == 0)); then | |
cat <<EOF | |
Usage: $self url | |
Example: $self http://localhost:3000/echo | |
Check if a URL is returning a valid WebSocket handshake response. | |
This is useful for healthcheck and liveness probes. | |
Note: Use an http/https for the URL scheme instead of ws/wss. | |
EOF | |
exit 1 | |
fi | |
url=$1 | |
code=`curl \ | |
--silent \ | |
--header "Upgrade: websocket" \ | |
--header "Connection: upgrade" \ | |
--header "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==" \ | |
--header "Sec-WebSocket-Version: 13" \ | |
--max-time 1 \ | |
--write "%{http_code}" \ | |
"$url"` | |
if [ "$code" = "101" ]; then | |
echo "it's alive" | |
exit 0 | |
else | |
echo "it's dead" | |
exit 1 | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment