I am trying to implement simple WebSocket program using Java Web Application.
However, not able to establish communication between client and server.
Can anybody help me?
Web Server: Tomcat
client code: jsp/javascrip
<body>
<div>
<input type="text" value="" id="message" /> <br /> <input
type="submit" value="Start" onclick="start()" />
</div>
<div id="messages"></div>
<script type="text/javascript">
var webSocket;
var uri = 'ws://' + window.location.host + '/ZebraHosting/testwebsocket';
alert('ur url is ' + uri);
function connect() {
if ('WebSocket' in window) {
alert('I am in Websocket in window');
websocket = new WebSocket(uri);
} else if ('MozWebSocket' in window) {
websocket = new MozWebSocket(uri);
alert('I am in MozWebsocket in window');
} else {
alert('WebSocket is not supported by this browser.');
return;
}
webSocket.onerror = function(event) {
alert('I am onerror');
onError(event);
};
webSocket.onopen = function(event) {
alert('I am onopen');
onOpen(event);
};
webSocket.onmessage = function(event) {
alert('I am onmessage');
onMessage(event);
};
webSocket.onclose = function(event) {
alert('I am onclose');
onClose(event);
};
}
function onMessage(event) {
document.getElementById('messages').innerHTML += '<br />'
+ event.data;
}
function onOpen(event) {
alert("function onOpen " );
document.getElementById('messages').innerHTML = 'Connection established';
}
function onError(event) {
alert("Error ocurred " );
}
function start() {
alert("function start " );
webSocket.send(document.getElementById('message').value);
return false;
}
function onClose(event) {
alert("function onClose" );
document.getElementById('messages').innerHTML = 'Connection closed';
}
connect();
</script>
Server Code:
import java.io.IOException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/testwebsocket")
public class WebSocketTest {
@OnMessage
public void onMessage(String message, Session session) throws IOException, InterruptedException {
// Print the client message for testing purposes
System.out.println("Received: " + message);
// Send the first message to the client
session.getBasicRemote().sendText("replay from server for :" + message);
}
@OnOpen
public void onOpen() {
System.out.println("Client connected");
}
@OnClose
public void onClose() {
System.out.println("Connection closed");
}}
asked Feb 16, 2016 at 13:00
School Boy
1,1832 gold badges20 silver badges34 bronze badges
-
so what logs are printed on client and server?wero– wero2016年02月16日 13:06:01 +00:00Commented Feb 16, 2016 at 13:06
-
@wero No logs as of now, I think nothing works after "function start()"School Boy– School Boy2016年02月16日 13:08:23 +00:00Commented Feb 16, 2016 at 13:08
-
I'll post you tomorrow a working example (if it's not already solved).Gero– Gero2016年02月16日 22:14:18 +00:00Commented Feb 16, 2016 at 22:14
-
@Gero Yes We are still working on it.School Boy– School Boy2016年02月17日 04:55:40 +00:00Commented Feb 17, 2016 at 4:55
2 Answers 2
You have typos in your code: You create WebSocket objects and assign it to variable websocket but later use variable webSocket.
answered Feb 16, 2016 at 13:13
wero
33.1k3 gold badges63 silver badges87 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
School Boy
Thanks for catch but still no output. one more question I have is the server class is normal Java class in this case do I need to deploy the war file every time? as what I am doing currently is just executing the above jsp page from eclipse.
School Boy
by updating Typo webSocket --> websocket, program run correctly.
I think @ApplicationScoped annotation is missing for server side class.
See this tutorial http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/HomeWebsocket/WebsocketHome.html
answered Feb 17, 2016 at 5:17
Ivan Dubynets
4311 gold badge5 silver badges15 bronze badges
2 Comments
School Boy
Thanks for the detailed code, let me try and test. will feedback in any case [PASS/FAIL]
School Boy
hi Ivan, by updating Typo webSocket --> websocket, program run correctly.
lang-java