Skip to content

Instantly share code, notes, and snippets.

@vppillai
Created October 24, 2014 04:19
Show Gist options
  • Save vppillai/20c71cd9c23df3534e90 to your computer and use it in GitHub Desktop.
Save vppillai/20c71cd9c23df3534e90 to your computer and use it in GitHub Desktop.
websocketFinal.sh
#!/bin/bash
stty -F /dev/ttyUSB0 speed 115200 cs8 -cstopb -parenb
coproc d { nc -l -p 81 -q 0; }
echo $$
until read line; line=`tr -d '\r\n'<<<$line`; test -z "$line"; do
test "${line:0:18}" = "Sec-WebSocket-Key:" && key=${line:19}
test "${line:0:22}" = "Sec-WebSocket-Version:" && ver=$line
done <&${d[0]}
rkey=`echo -n ${key}258EAFA5-E914-47DA-95CA-C5AB0DC85B11|openssl dgst -sha1 -binary|base64`
echo -ne "HTTP/1.1 101 Switching Protocols\r\n" >&${d[1]}
echo -ne "Upgrade: websocket\r\nConnection: Upgrade\r\n" >&${d[1]}
echo -ne "Sec-WebSocket-Accept: $rkey\r\n$ver\r\n\r\n" >&${d[1]}
while true; do
sleep 5
while read line
do
if [ ! -z "$line" ]; then
size=`expr ${#line} + 2`;
printf -v hexSize "%x" "$size"
printf "\\x81\\x$hexSize$line\r\n" >&${d[1]}
# sleep 1
fi
done </dev/ttyUSB0
done
#while true; do
#echo -ne "\x81\x7hello\r\n" >&${d[1]}
#sleep 1
#echo -ne "\x81\x7mello\r\n" >&${d[1]}
#sleep 1
#done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment