➜ temp wget http://download.websocketd.com/releases/websocketd/0.2.7/darwin_amd64/websocketd
--2013-12-13 14:53:14-- http://download.websocketd.com/releases/websocketd/0.2.7/darwin_amd64/websocketd
Resolving download.websocketd.com... 176.32.98.233
Connecting to download.websocketd.com|176.32.98.233|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5970144 (5.7M) [application/octet-stream]
Saving to: ‘websocketd’
100%[===========================================================>] 5,970,144 962KB/s in 11s
2013-12-13 14:53:27 (551 KB/s) - ‘websocketd’ saved [5970144/5970144]
➜ temp chmod +x websocketd
Test it:
➜ temp ./websocketd --help
websocketd (0.2.7)
websocketd is a command line tool that will allow any executable program
that accepts input on stdin and produces output on stdout to be turned into
a WebSocket server....
create an app
count.sh:
#!/bin/bash
# Count from 1 to 10, pausing for a second between each iteration.
for COUNT in $(seq 1 10); do
echo $COUNT
sleep 1
done
Make it executable:
$ chmod +x ./count.sh
Start the server:
➜ websockets ./websocketd --port=8080 ./count.sh
Fri, 13 Dec 2013 14:58:11 +1100 | INFO | server | | Starting WebSocket server : ws://0.0.0.0:8080/
Fri, 13 Dec 2013 14:58:11 +1100 | INFO | server | | Serving using application : ./count.sh
In a web-page:
var ws = new WebSocket('ws://localhost:8080/');
ws.onmessage = function(event) {
console.log('Count is: ' + event.data);
};
bam:
Server logs:
➜ websockets ./websocketd --port=8080 ./count.sh
Fri, 13 Dec 2013 14:58:11 +1100 | INFO | server | | Starting WebSocket server : ws://0.0.0.0:8080/
Fri, 13 Dec 2013 14:58:11 +1100 | INFO | server | | Serving using application : ./count.sh
Fri, 13 Dec 2013 14:59:38 +1100 | ACCESS | session | url:'http://[::1]:56042/' remote:'localhost' id:'1386907178885478031' origin:'http://www.blogger.com' | CONNECT
Fri, 13 Dec 2013 14:59:48 +1100 | ACCESS | session | url:'http://[::1]:56042/' remote:'localhost' id:'1386907178885478031' origin:'http://www.blogger.com' command:'./count.sh' pid:'46495' | DISCONNECT
test it with --devconsole
./websocketd --port=8080 --devconsole ./count.sh
and open http://localhost:8080/
connect with an javascript WebSocket
create a file count.html:
<!DOCTYPE html>
<html>
<head>
<title>websocketd count example</title>
<style>
#count {
font: bold 150px arial;
margin: auto;
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<div id="count"></div>
<script>
var ws = new WebSocket('ws://localhost:8080/');
ws.onopen = function() {
document.body.style.backgroundColor = '#cfc';
};
ws.onclose = function() {
document.body.style.backgroundColor = null;
};
ws.onmessage = function(event) {
document.getElementById('count').innerText = event.data;
};
</script>
</body>
</html>
Open this page in your web-browser. It will even work if you open it directly from disk using a
file://
URL.Test it with greeter.sh
make a file greeter.sh
while read LINE
do
echo "Hello $LINE!"
done
and run it
➜ temp ./websocketd --port=8080 --devconsole ./greeter.sh
Fri, 13 Dec 2013 15:30:02 +1100 | INFO | server | | Starting WebSocket server : ws://0.0.0.0:8080/
Fri, 13 Dec 2013 15:30:02 +1100 | INFO | server | | Developer console enable d : http://0.0.0.0:8080/
Fri, 13 Dec 2013 15:30:02 +1100 | INFO | server | | Serving using application : ./greeter.sh
open console
http://localhost:8080/
connect and type your name
send>> "Yannis"
onmessage: Hello "Yannis"!
UPDATE:
use this small example to run remote terminal commands
https://github.com/msroot/SockeTerm
https://github.com/joewalnes/websocketd/wiki/Ten-second-tutorial
No comments:
Post a Comment