2

I have been reading up on the WebSocket api and have been playing around with it a little bit but I can not get my head wrapped around a few things. Right now I have a servlet TestServlet which is defined in web.xml to trigger the doGet method every time the URL localhost:TestServlet is put into the URL of a browser.

I want to wait for a connection from a mobile device, the guy building the phone app says they can load my servlet, in which case the doGet method would trigger and I would do my business with the data received. Only issue is that in the doGet method I have the request and the response, can I get the data sting sent from the phone from the request?

I also have my @ServerEndpoint set to /* which I think means the servlet will be active when a url containing /TestServlet is loaded. Problem here is I do not see any output from print lines or see any breakpoints hit.

I am just wondering (if this makes any sense) having the @ServerEndpoint set as @ServerEndpoint(value = "/*") will the method annotated @OnMessage trigger if a message is there to receive?

If not how can I make it trigger, and if I can not how can I get a data string from an HttpServletRequest?

BalusC
1.1m377 gold badges3.7k silver badges3.6k bronze badges
asked Jan 2, 2015 at 4:34

1 Answer 1

1

You're mixing basic concepts. The @ServerEndpoint(value) does not represent an URL pattern like @WebServlet(urlPatterns). It represents the URI or URI template. The value of /* is just plain wrong. You should not think of it as a servlet (filter). It is not "automagically" invoked on certain http:// requests either. The client has to explicitly fire a ws:// request by a.o. new WebSocket(uri) in JavaScript (assuming a web application).

E.g.

@ServerEndpoint("/push")
public class Push {}

with

if (window.WebSocket) {
 var ws = new WebSocket("ws://example.com/contextname/push");
 ws.onmessage = function(event) {
 var text = event.data;
 console.log(text);
 };
}
else {
 // Bad luck. Browser doesn't support it. Consider falling back to long polling.
 // See http://caniuse.com/websockets for an overview of supported browsers.
 // There exist jQuery WebSocket plugins with transparent fallback.
}

See also:


Update: it turns out to be a true mobile app (Android/iOS). If the (native) URL/HTTP connection library being used to connect the Servlet by http:// does not support the ws:// protocol, then you'd best look for a 3rd party library supporting that. To find one, use keywords "android websocket client", or "ios websocket client". Note: in documentation/tutorials/blogs, pay attention to the "client" part, not the "server" part, because the "server" part is already implemented by @ServerEndpoint.

answered Jan 2, 2015 at 9:57
Sign up to request clarification or add additional context in comments.

4 Comments

I deleted my previous comment as I had updated it to: Now if I understand you correctly, the javascript would run on a page and initialize Socket with an event. For testing I could make a dummy page with just that code on it and test my listener. Putting this code on the main page of my website would start the socket when a user navigates to the site, though I fail to see how a phone could send updates to the server over this socket.
Needless to say I think you have answered my question in full. Get the WS jar files for android, install them and set up the connection on the phone. This is perfect and I thank you greatly!
This is great, it looks like I can use the jars to create a stand alone client and use it to connect to the server. As well we can put this on the phone! Now this is slightly unrelated but I think you could help. Where can I upload my server or register my site with the DNS, I do not want to have to connect using ip:port/ProjectName/index.xhtml, any suggestions?
@BalusC: @ServerEndpoint("/user") caters to requests that are going to the url .../user. Let's say we have two requests that go to /user with the only difference being the message being sent. One message says create and another says delete. How do you indicate to the rest of the class if it should create or delete if all you have is one url to define everything?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.