websocket
This commit is contained in:
parent
00b3ad3038
commit
9a3f74e8c0
3 changed files with 4807 additions and 0 deletions
4727
Application/WebContent/WEB-INF/web.xml
Normal file
4727
Application/WebContent/WEB-INF/web.xml
Normal file
File diff suppressed because it is too large
Load diff
47
Application/WebContent/WebSocketClient.html
Normal file
47
Application/WebContent/WebSocketClient.html
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Tomcat WebSocket</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form>
|
||||||
|
<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/WebSocketServer/websocketendpoint");
|
||||||
|
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 sended to the server : " + message.value + "\n";
|
||||||
|
message.value = "";
|
||||||
|
}
|
||||||
|
function wsCloseConnection(){
|
||||||
|
webSocket.close();
|
||||||
|
}
|
||||||
|
function wsGetMessage(message){
|
||||||
|
echoText.value += "Message received from to the server : " + message.data + "\n";
|
||||||
|
}
|
||||||
|
function wsClose(message){
|
||||||
|
echoText.value += "Disconnect ... \n";
|
||||||
|
}
|
||||||
|
|
||||||
|
function wsError(message){
|
||||||
|
echoText.value += "Error ... \n";
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
33
Application/src/server/ws/server.java
Normal file
33
Application/src/server/ws/server.java
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
package server.ws;
|
||||||
|
import javax.websocket.OnClose;
|
||||||
|
import javax.websocket.OnError;
|
||||||
|
import javax.websocket.OnMessage;
|
||||||
|
import javax.websocket.OnOpen;
|
||||||
|
import javax.websocket.server.ServerEndpoint;
|
||||||
|
|
||||||
|
@ServerEndpoint("/websocketendpoint")
|
||||||
|
public class server {
|
||||||
|
|
||||||
|
|
||||||
|
@OnOpen
|
||||||
|
public void onOpen(){
|
||||||
|
System.out.println("Open Connection ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnClose
|
||||||
|
public void onClose(){
|
||||||
|
System.out.println("Close Connection ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnMessage
|
||||||
|
public String onMessage(String message){
|
||||||
|
System.out.println("Message from the client: " + message);
|
||||||
|
String echoMsg = "Echo from the server : " + message;
|
||||||
|
return echoMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnError
|
||||||
|
public void onError(Throwable e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue