mirror of
https://github.com/eddyem/onionserver.git
synced 2026-03-20 00:31:01 +03:00
Add websocket example
This commit is contained in:
@@ -10,8 +10,9 @@ auth = function(){
|
||||
$("inout").onclick = auth.logout;
|
||||
}
|
||||
function _wsk(request){
|
||||
var wsKey = request.responseText;
|
||||
wsKey = request.responseText;
|
||||
if(wsKey) console.log("Web key received: " + wsKey);
|
||||
wsinit();
|
||||
}
|
||||
function reqAuth(request){
|
||||
var txt = request.responseText;
|
||||
@@ -62,10 +63,34 @@ auth = function(){
|
||||
var str = "auth/?login=" + l + "&passwd=" + p;
|
||||
sendrequest(str, reqAuth);
|
||||
}
|
||||
// websockets
|
||||
var ws;
|
||||
function wsinit(){
|
||||
delete(ws);
|
||||
ws = new WebSocket('wss://localhost:8081');
|
||||
ws.onopen = function(){ws.send("Akey="+wsKey);}; // send key after init
|
||||
ws.onclose = function(evt){
|
||||
var text = "WebSocket closed: ";
|
||||
if(evt.wasClean) text += "by remote side";
|
||||
else text += "connection lost"
|
||||
$('wsmsgs').innerHTML = text;
|
||||
};
|
||||
ws.onmessage = function(evt){
|
||||
$('wsmsgs').innerHTML = evt.data;
|
||||
}
|
||||
ws.onerror = function(err){
|
||||
parseErr("WebSocket error " + err.message);
|
||||
}
|
||||
}
|
||||
function wssend(txt){
|
||||
ws.send(txt);
|
||||
}
|
||||
return{
|
||||
init: init1,
|
||||
login: login1,
|
||||
logout: logout1,
|
||||
send: sendlogpass
|
||||
send: sendlogpass,
|
||||
wssend: wssend,
|
||||
wsinit: wsinit
|
||||
};
|
||||
}();
|
||||
|
||||
@@ -7,7 +7,11 @@
|
||||
<body onload="auth.init();">
|
||||
<p>Text
|
||||
<p>More text
|
||||
<button onclick="auth.wsinit();">Push me</button>
|
||||
<p>
|
||||
<div id="wsmsgs"></div>
|
||||
<div id="errmsg" style='background-color: red;'></div>
|
||||
<p>
|
||||
<input type="text" id="wssnd" onchange="auth.wssend($('wssnd').value);">
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user