mirror of
https://github.com/eddyem/eddys_snippets.git
synced 2025-12-06 18:55:16 +03:00
127 lines
3.4 KiB
HTML
127 lines
3.4 KiB
HTML
<html>
|
|
<head>
|
|
<title>Testing websockets</title>
|
|
<style type="text/css">
|
|
.content { vertical-align:top; text-align:center; background:#fffff0; padding:12px; border-radius:10px; }
|
|
button { display: block; width: 70px; text-align: center; }
|
|
.container {border: solid 1px; border-radius:10px; padding:12px; }
|
|
</style>
|
|
<script type="text/javascript" src="./md5.js" language="javascript"></script>
|
|
<script type="text/javascript" src="./localstore.js" language="javascript"></script>
|
|
<script type="text/javascript" src="./getpass.js" language="javascript"></script>
|
|
<script type="text/javascript">
|
|
(function () { 'use strict';
|
|
var socket;
|
|
var pwhash = "", salt = "";
|
|
var connected = 0, verified = 0;
|
|
var wdTmout, faulttmout;
|
|
function $(nm){return document.getElementById(nm);}
|
|
function get_ws_url(){
|
|
var pcol;
|
|
var u = document.URL;
|
|
if (u.substring(0, 5) == "https") {
|
|
pcol = "wss://";
|
|
u = u.substr(8);
|
|
} else {
|
|
pcol = "ws://";
|
|
if (u.substring(0, 4) == "http")
|
|
u = u.substr(7);
|
|
}
|
|
u = u.split('/');
|
|
return pcol + u[0] + ":9999";
|
|
}
|
|
function TryConnect(){
|
|
if(connected) return;
|
|
//if(socket) delete socket;
|
|
var apprURL = get_ws_url();
|
|
var sockfn = null;
|
|
if(typeof MozWebSocket != "undefined")
|
|
sockfn = MozWebSocket;
|
|
else
|
|
sockfn = WebSocket;
|
|
socket = new sockfn(apprURL, "IRBIS_control");
|
|
if(!socket){
|
|
alert("Error: can't create websocket!\nMake sure that your browser supports websockets");
|
|
return;
|
|
}
|
|
function chkdata(msg){
|
|
if(msg.substr(0,6) == "pwhash"){
|
|
salt = msg.substr(6);
|
|
console.log("salt="+salt);
|
|
console.log("md5="+md5(salt+pwhash));
|
|
if(pwhash != "") socket.send(md5(salt+pwhash));
|
|
else{
|
|
console.log("empty password!");
|
|
GetPass.get();
|
|
return;
|
|
}
|
|
}
|
|
else if(msg == "authOK") verified = 1;
|
|
else if(msg == "badpass"){
|
|
verified = 0;
|
|
alert("Wrong password!");
|
|
pwhash == "";
|
|
GetPass.get();
|
|
}
|
|
}
|
|
try {
|
|
socket.onopen = function(){
|
|
$("connected").style.backgroundColor = "#40ff40";
|
|
$("connected").textContent = "Connection opened";
|
|
connected = 1;
|
|
verified = 0;
|
|
salt = "";
|
|
}
|
|
socket.onmessage = function(msg){
|
|
var answ = msg.data;
|
|
console.log("got: "+answ);
|
|
if(!verified) chkdata(answ);
|
|
else $("lastmsg").textContent = answ;
|
|
}
|
|
socket.onclose = function(){
|
|
$("connected").style.backgroundColor = "#ff4040";
|
|
$("connected").textContent = "Connection closed";
|
|
$("lastmsg").textContent = "";
|
|
connected = 0;
|
|
verified = 0;
|
|
setTimeout(TryConnect, 1000);
|
|
socket = null;
|
|
}
|
|
} catch(exception) {
|
|
alert('Error' + exception);
|
|
}
|
|
}
|
|
function calcHash(pass){
|
|
pwhash = md5(pass)
|
|
console.log("MD5: " + pwhash);
|
|
Storage.save("pwhash", pwhash);
|
|
if(salt != "" && socket)
|
|
socket.send(md5(salt+pwhash));
|
|
}
|
|
function init(){
|
|
$("sendmsg").onclick = sendmsg;
|
|
pwhash = Storage.load("pwhash", "");
|
|
GetPass.onenter = calcHash;
|
|
TryConnect();
|
|
}
|
|
function sendmsg(){
|
|
if(connected) socket.send($("msgtosend").value);
|
|
$("md5").textContent = md5($("msgtosend").value);
|
|
}
|
|
var WStest = {};
|
|
WStest.init = init;
|
|
WStest.send = sendmsg;
|
|
window.WStest = WStest;
|
|
}());
|
|
</script>
|
|
</head>
|
|
<body onload="WStest.init()">
|
|
<div class="container" id="cont">
|
|
<div><input type="text" id="msgtosend" size="15"><button id="sendmsg">Send</button></div>
|
|
<div id="connected">No connection</div>
|
|
<div id="lastmsg"></div>
|
|
<div>Message MD5 sum: <span id="md5"></span></div>
|
|
</div>
|
|
</body>
|
|
</html>
|