Leser: 12
InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> status</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script> <script type="text/javascript" src="https://cdn.rawgit.com/emn178/js-sha256/master/src/sha256.js"> </script> <script> function WebSocketAuth(ws) { this.wss = ws; this.send2 = function(msg) { var d = new Date(); var nonce = d.getTime(); var secret = 'secret'; var sig = sha256(secret + nonce); var json = JSON.stringify({ "message":msg, "nonce":nonce, "sig":sig }); this.wss.send(json); }; } var wss = new WebSocket('ws://181.174.166.70:3000/echo'); var wsa = new WebSocketAuth( wss ); $(function () { $('#msg').focus(); var log = function (text) { $('#log').val( $('#log').val() + text + "\n"); }; wss.onopen = function () { log('Connection opened.'); }; wss.onmessage = function (msg) { var res = JSON.parse(msg.data); log('[' + res.hms + '] ' + res.text); }; }); $(function () { wsa.send2( 'test' ); }); $(function( ) { $('#msg').keydown(function (e) { if (e.keyCode == 13 && $('#msg').val()) { var msg = $('#msg').val(); wsa.send2( msg ); $('#msg').val(''); } }); }); </script> <script> wsa.send2( 'test' ); </script> </head> <body> <div > <p><input type="text" id="msg" /> <textarea id="log" readonly></textarea></p> </div> </body> </html>
QuoteDu hast davon mehrere, warum, wozu!?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<script>
function WebSocketAuth(ws) {
this.wss = ws;
this.send2 = function(msg) {
var d = new Date();
var nonce = d.getTime();
var secret = 'secret';
var sig = sha256(secret + nonce);
var json = JSON.stringify({
"message":msg,
"nonce":nonce,
"sig":sig
});
this.wss.send(json);
};
}
var wss = new WebSocket('ws://181.174.166.70:3000/echo');
var wsa = new WebSocketAuth( wss );
$(function () {
$('#msg').focus();
var log = function (text) {
$('#log').val( $('#log').val() + text + "\n");
};
wss.onopen = function () {
log('Connection opened.');
};
wss.onmessage = function (msg) {
var res = JSON.parse(msg.data);
log('[' + res.hms + '] ' + res.text);
};
// wsa.send2( "test" );
$('#msg').keydown(function (e) {
if (e.keyCode == 13 && $('#msg').val()) {
var msg = $('#msg').val();
wsa.send2( msg );
$('#msg').val('');
}
});
});
</script>
1
2
3
Uncaught DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
at WebSocketAuth.send2 (http://181.174.166.70/:27:13)
at http://181.174.166.70/:63:6
setTimeout(function(){ wsa.send2( "test" ); }, 3000);