nst-0x08/websocketspls/WebContent/index.html

58 lines
1.5 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Tomcat WebSocket Demo</title>
</head>
<body>
<form onsubmit="javascript:wsSendMessage(); return false;">
<input id="message" type="text">
<input onclick="wsSendMessage();" value="Echo" type="button">
<input onclick="wsCloseConnection();" value="Disconnect" type="button">
</form>
<br>
<textarea id="echoText" rows="5" cols="30"></textarea>
<script type="text/javascript">
var webSocket = new WebSocket("ws://localhost:8080/websocketspls/echo");
var echoText = document.getElementById("echoText");
echoText.value = "";
var message = document.getElementById("message");
webSocket.onopen = function(message) {
wsOpen(message);
};
webSocket.onmessage = function(message) {
wsGetMessage(message);
};
webSocket.onclose = function(message) {
wsClose(message);
};
webSocket.onerror = function(message) {
wsError(message);
};
function wsOpen(message) {
echoText.value += "Connected ... \n";
}
function wsSendMessage() {
webSocket.send(message.value);
echoText.value += "Message sent: " + message.value
+ "\n";
message.value = "";
}
function wsCloseConnection() {
webSocket.close();
}
function wsGetMessage(message) {
echoText.value += "Message received: "
+ message.data + "\n";
}
function wsClose(message) {
echoText.value += "Disconnect ... \n";
}
function wsError(message) {
echoText.value += "Error ... \n";
}
</script>
</body>
</html>