| 1 |
if (!window.WebSocket && window.MozWebSocket) { |
| 2 |
window.WebSocket = window.MozWebSocket; |
| 3 |
} |
| 4 |
|
| 5 |
if (!window.WebSocket) { |
| 6 |
alert("WebSocket not supported by this browser"); |
| 7 |
} |
| 8 |
|
| 9 |
function $() { |
| 10 |
return document.getElementById(arguments[0]); |
| 11 |
} |
| 12 |
function $F() { |
| 13 |
return document.getElementById(arguments[0]).value; |
| 14 |
} |
| 15 |
|
| 16 |
function getKeyCode(ev) { |
| 17 |
if (window.event) |
| 18 |
return window.event.keyCode; |
| 19 |
return ev.keyCode; |
| 20 |
} |
| 21 |
|
| 22 |
var wstool = { |
| 23 |
connect : function() { |
| 24 |
var location = document.location.toString().replace('http://', 'ws://') |
| 25 |
.replace('https://', 'wss://'); |
| 26 |
|
| 27 |
wstool.info("Document URI: " + document.location); |
| 28 |
wstool.info("WS URI: " + location); |
| 29 |
|
| 30 |
this._scount = 0; |
| 31 |
|
| 32 |
try { |
| 33 |
this._ws = new WebSocket(location, "tool"); |
| 34 |
this._ws.onopen = this._onopen; |
| 35 |
this._ws.onmessage = this._onmessage; |
| 36 |
this._ws.onclose = this._onclose; |
| 37 |
} catch (exception) { |
| 38 |
wstool.info("Connect Error: " + exception); |
| 39 |
} |
| 40 |
}, |
| 41 |
|
| 42 |
close : function() { |
| 43 |
this._ws.close(); |
| 44 |
}, |
| 45 |
|
| 46 |
_out : function(css, message) { |
| 47 |
var console = $('console'); |
| 48 |
var spanText = document.createElement('span'); |
| 49 |
spanText.className = 'text ' + css; |
| 50 |
spanText.innerHTML = message; |
| 51 |
var lineBreak = document.createElement('br'); |
| 52 |
console.appendChild(spanText); |
| 53 |
console.appendChild(lineBreak); |
| 54 |
console.scrollTop = console.scrollHeight - console.clientHeight; |
| 55 |
}, |
| 56 |
|
| 57 |
info : function(message) { |
| 58 |
wstool._out("info", message); |
| 59 |
}, |
| 60 |
|
| 61 |
infoc : function(message) { |
| 62 |
if(message.length > 300) { |
| 63 |
wstool._out("client", "[c] [big message: " + message.length + " characters]"); |
| 64 |
} else { |
| 65 |
wstool._out("client", "[c] " + message); |
| 66 |
} |
| 67 |
}, |
| 68 |
|
| 69 |
infos : function(message) { |
| 70 |
this._scount++; |
| 71 |
if(message.length > 300 && !message.startsWith("Dump:")) { |
| 72 |
wstool._out("server", "[s" + this._scount + "] [big message: " + message.length + " characters]"); |
| 73 |
} else { |
| 74 |
wstool._out("server", "[s" + this._scount + "] " + message); |
| 75 |
} |
| 76 |
}, |
| 77 |
|
| 78 |
setState : function(enabled) { |
| 79 |
$('connect').disabled = enabled; |
| 80 |
$('close').disabled = !enabled; |
| 81 |
$('info').disabled = !enabled; |
| 82 |
$('ping').disabled = !enabled; |
| 83 |
$('time').disabled = !enabled; |
| 84 |
$('many').disabled = !enabled; |
| 85 |
$('manythreads').disabled = !enabled; |
| 86 |
$('hello').disabled = !enabled; |
| 87 |
$('there').disabled = !enabled; |
| 88 |
$('dump').disabled = !enabled; |
| 89 |
$('json').disabled = !enabled; |
| 90 |
$('send10k').disabled = !enabled; |
| 91 |
$('send100k').disabled = !enabled; |
| 92 |
$('send1000k').disabled = !enabled; |
| 93 |
$('send10m').disabled = !enabled; |
| 94 |
}, |
| 95 |
|
| 96 |
_onopen : function() { |
| 97 |
wstool.setState(true); |
| 98 |
wstool.info("Websocket Connected"); |
| 99 |
}, |
| 100 |
|
| 101 |
_send : function(message) { |
| 102 |
if (this._ws) { |
| 103 |
this._ws.send(message); |
| 104 |
wstool.infoc(message); |
| 105 |
} |
| 106 |
}, |
| 107 |
|
| 108 |
write : function(text) { |
| 109 |
wstool._send(text); |
| 110 |
}, |
| 111 |
|
| 112 |
_onmessage : function(m) { |
| 113 |
if (m.data) { |
| 114 |
wstool.infos(m.data); |
| 115 |
} |
| 116 |
}, |
| 117 |
|
| 118 |
_onclose : function(closeEvent) { |
| 119 |
this._ws = null; |
| 120 |
wstool.setState(false); |
| 121 |
wstool.info("Websocket Closed"); |
| 122 |
wstool.info(" .wasClean = " + closeEvent.wasClean); |
| 123 |
|
| 124 |
var codeMap = {}; |
| 125 |
codeMap[1000] = "(NORMAL)"; |
| 126 |
codeMap[1001] = "(ENDPOINT_GOING_AWAY)"; |
| 127 |
codeMap[1002] = "(PROTOCOL_ERROR)"; |
| 128 |
codeMap[1003] = "(UNSUPPORTED_DATA)"; |
| 129 |
codeMap[1004] = "(UNUSED/RESERVED)"; |
| 130 |
codeMap[1005] = "(INTERNAL/NO_CODE_PRESENT)"; |
| 131 |
codeMap[1006] = "(INTERNAL/ABNORMAL_CLOSE)"; |
| 132 |
codeMap[1007] = "(BAD_DATA)"; |
| 133 |
codeMap[1008] = "(POLICY_VIOLATION)"; |
| 134 |
codeMap[1009] = "(MESSAGE_TOO_BIG)"; |
| 135 |
codeMap[1010] = "(HANDSHAKE/EXT_FAILURE)"; |
| 136 |
codeMap[1011] = "(SERVER/UNEXPECTED_CONDITION)"; |
| 137 |
codeMap[1015] = "(INTERNAL/TLS_ERROR)"; |
| 138 |
var codeStr = codeMap[closeEvent.code]; |
| 139 |
wstool.info(" .code = " + closeEvent.code + " " + codeStr); |
| 140 |
wstool.info(" .reason = " + closeEvent.reason); |
| 141 |
} |
| 142 |
}; |