Skip to content

Instantly share code, notes, and snippets.

@droidScriptKiddy
Created March 15, 2020 00:27
Show Gist options
  • Save droidScriptKiddy/8bc282b18d3342c3b641465c87af5df8 to your computer and use it in GitHub Desktop.
Save droidScriptKiddy/8bc282b18d3342c3b641465c87af5df8 to your computer and use it in GitHub Desktop.
Web Socket Performance Test // source https://jsbin.com/wajosoliso
<!doctype html>
<title>Web Socket Performance Test</title>
<form onsubmit="return !1;">
<h3>WebSocket Monitor</h3>
<label>Host:</label>
<input type="text" id="host" value="" style="width:160px;" placeholder="ws://hostname:port/ws"/>
<input type="button" value="Connect" onclick="connect(host.value)" />
<label>Status:</label>
<input type="text" id="connectionLabel" readonly="true" value="Idle"/>
<br />
<textarea id="input" style="width:100%;height:100px;"></textarea>
<br>
<textarea id="output" style="width:100%;height:300px;"></textarea>
<br>
<input type="button" value="Clear" onclick="clearText()">
</form>
<cite>Source: <a href="https://github.com/tzapu/WebSocketSerialMonitor">https://github.com/tzapu/WebSocketSerialMonitor</a> to see how to get started.</cite>
<script type="text/javascript">
const PING_INTERVAL_MILLIS = 5000;
var output = document.getElementById('output');
var connectionLabel = document.getElementById('connectionLabel');
var socket;
function connect(host) {
console.log('connect', host);
if (window.WebSocket) {
connectionLabel.value = "Connecting";
if(socket) {
socket.close();
socket = null;
}
socket = new WebSocket(host);
socket.onmessage = function(event) {
output.value += event.data; // + "\r\n";
var textarea = document.getElementById('output');
textarea.scrollTop = textarea.scrollHeight;
};
socket.onopen = function(event) {
isRunning = true;
connectionLabel.value = "Connected";
};
socket.onclose = function(event) {
isRunning = false;
connectionLabel.value = "Disconnected";
// socket.removeAllListeners();
// socket = null;
};
socket.onerror = function(event) {
connectionLabel.value = "Error";
// socket.removeAllListeners();
// socket = null;
};
} else {
alert("Your browser does not support Web Socket.");
}
}
/*window.setInterval(function() {
send("ping");
}, PING_INTERVAL_MILLIS);
*/
function send(data) {
if (!window.WebSocket) {
return;
}
if (socket.readyState == WebSocket.OPEN) {
var message = data;
output.value += 'sending : ' + data + '\r\n';
socket.send(message);
} else {
alert("The socket is not open.");
}
}
function clearText() {
output.value="";
}
</script>
<script id="jsbin-source-html" type="text/html"><!doctype html>
<title>Web Socket Performance Test</title>
<form onsubmit="return !1;">
<h3>WebSocket Monitor</h3>
<label>Host:</label>
<input type="text" id="host" value="" style="width:160px;" placeholder="ws://hostname:port/ws"/>
<input type="button" value="Connect" onclick="connect(host.value)" />
<label>Status:</label>
<input type="text" id="connectionLabel" readonly="true" value="Idle"/>
<br />
<textarea id="input" style="width:100%;height:100px;"></textarea>
<br>
<textarea id="output" style="width:100%;height:300px;"></textarea>
<br>
<input type="button" value="Clear" onclick="clearText()">
</form>
<cite>Source: <a href="https://github.com/tzapu/WebSocketSerialMonitor">https://github.com/tzapu/WebSocketSerialMonitor</a> to see how to get started.</cite>
<script type="text/javascript">
const PING_INTERVAL_MILLIS = 5000;
var output = document.getElementById('output');
var connectionLabel = document.getElementById('connectionLabel');
var socket;
function connect(host) {
console.log('connect', host);
if (window.WebSocket) {
connectionLabel.value = "Connecting";
if(socket) {
socket.close();
socket = null;
}
socket = new WebSocket(host);
socket.onmessage = function(event) {
output.value += event.data; // + "\r\n";
var textarea = document.getElementById('output');
textarea.scrollTop = textarea.scrollHeight;
};
socket.onopen = function(event) {
isRunning = true;
connectionLabel.value = "Connected";
};
socket.onclose = function(event) {
isRunning = false;
connectionLabel.value = "Disconnected";
// socket.removeAllListeners();
// socket = null;
};
socket.onerror = function(event) {
connectionLabel.value = "Error";
// socket.removeAllListeners();
// socket = null;
};
} else {
alert("Your browser does not support Web Socket.");
}
}
/*window.setInterval(function() {
send("ping");
}, PING_INTERVAL_MILLIS);
*/
function send(data) {
if (!window.WebSocket) {
return;
}
if (socket.readyState == WebSocket.OPEN) {
var message = data;
output.value += 'sending : ' + data + '\r\n';
socket.send(message);
} else {
alert("The socket is not open.");
}
}
function clearText() {
output.value="";
}
<\/script>
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment