52 lines
1.2 KiB
Bash
Executable file
52 lines
1.2 KiB
Bash
Executable file
#!/bin/sh
|
|
trap "stty sane; trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
|
|
PORT=9090
|
|
PROCFILE=/tmp/.xrsh
|
|
|
|
usage(){ echo "Usage: xrsh [--keyboard]"; exit; }
|
|
|
|
assert_cmds(){
|
|
for cmd in $1; do
|
|
command -v $cmd &>dev/null || { echo "error: please install $cmd (not found)"; exit 1; }
|
|
done
|
|
}
|
|
|
|
keyboard(){
|
|
assert_cmds websocat foobar
|
|
echo '
|
|
<h1>keyboard connector</h1>
|
|
|
|
<pre>
|
|
connect to ws://127.0.0.1:'$PORT'
|
|
</pre>
|
|
|
|
<script>
|
|
var ws = new WebSocket("ws://127.0.0.1:'$PORT'/");
|
|
|
|
ws.onmessage = function(event) {
|
|
if( !event.data ) return
|
|
event.data.arrayBuffer().then( (buf) => {
|
|
decoder = new TextDecoder("utf-8")
|
|
c = decoder.decode( new Uint8Array(buf) )
|
|
console.log(c)
|
|
})
|
|
};
|
|
</script>
|
|
' > /tmp/index.html
|
|
|
|
echo $$ > $PROCFILE
|
|
echo "warn: CTRL-C is forwarded too"
|
|
echo "warn: to exit this app run: $0 --kill"
|
|
stty raw
|
|
websocat -E -b -s $PORT -F /:text/html:/tmp/index.html
|
|
}
|
|
|
|
# launch!
|
|
case "$*" in
|
|
-h) usage ;;
|
|
--kill) pkill -P $(cat $PROCFILE); exit ;;
|
|
--help) usage ;;
|
|
--keyboard) keyboard ;;
|
|
--raw) RAW=1 ;;
|
|
esac
|
|
|