fly-socket/test.html

42 lines
1.5 KiB
HTML
Raw Normal View History

2019-07-29 21:20:54 +02:00
<!DOCTYPE html>
<html>
<head>
<title>WebSocket test</title>
</head>
<body>
<script>
function hex(str)
{
var arr1 = [];
for (var n = 0, l = str.length; n < l; n ++)
{
var hex = Number(str.charCodeAt(n)).toString(16);
arr1.push(hex);
}
return arr1.join(' ');
}
var ws = new WebSocket("ws://127.0.0.1:7767/"),
messages = document.createElement('ul');
ws.onmessage = function (event) {
var messages = document.getElementsByTagName('ul')[0],
message = document.createElement('li'),
res = new Response(event.data).arrayBuffer().then(
function(buff) {
last = buff;
data = new DataView(buff);
data_str = "";
for (let i = 0; i < data.byteLength; i++)
data_str += String.fromCharCode(data.getInt8(i));
content = document.createTextNode(data_str + " | " + hex(data_str));
message.appendChild(content);
messages.appendChild(message);
}
);
};
document.body.appendChild(messages);
</script>
</body>
</html>