Skip to content

Instantly share code, notes, and snippets.

@porst17
Last active September 2, 2015 10:14
Show Gist options
  • Save porst17/550f95595fad7f67a48f to your computer and use it in GitHub Desktop.
Save porst17/550f95595fad7f67a48f to your computer and use it in GitHub Desktop.
A basic example of connecting a website to a shell command using WebSockets and websockify
<!--
run on console:
websockify --wrap-mode=respawn 1234 -- bash -c 'cat | netcat -q -1 -k -l 1234'
replace 'cat' with the command of your choice
/-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 25 March 2009), see www.w3.org" />
<meta charset="utf-8" />
<title>WebSocket/WebSockify Test</title>
<script language="javascript" type="text/javascript">
//<![CDATA[
var wsUri = "ws://localhost:1234";
var output;
var input;
function init()
{
output = document.getElementById("output");
output.value = "";
input = document.getElementById("input");
testWebSocket();
}
function testWebSocket()
{
websocket = new WebSocket(wsUri, 'base64');
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
}
function onOpen(evt)
{
writeToScreen("CONNECTED\n");
doSend("WebSocket rocks\n");
}
function onClose(evt)
{
writeToScreen("DISCONNECTED\n");
}
function onMessage(evt)
{
writeToScreen('RECV: ' + atob( evt.data ) );
}
function onError(evt)
{
writeToScreen('ERR: ' + evt.data );
}
function doSend(message)
{
writeToScreen("SENT: " + message );
websocket.send( btoa( message ) );
}
function writeToScreen(message)
{
output.value += message;
}
window.addEventListener("load", init, false);
//]]>
</script>
</head>
<body>
<h2>WebSocket Test</h2>
<form>
<input type="text" id="input" size="50" /> <input type="button" value="Send"
onclick="doSend(input.value+'\n'); input.value='';" /><br />
<textarea id="output" rows="10" cols="100"></textarea>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment