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?
1 Answer 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:
- Real time updates from database using JSF/Java EE (for another concrete example involving websockets with JavaScript)
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.
4 Comments
@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?