|Re: [jetty-users] WebSockets: Specific Client Message|
Ok, so far. That is what i use already ;)But i get a problem with this example, if i use multiple clients respectively parallel requests/connections .
If two or more clients connect to the server and the server answers, each client get all answers.
For example: Client 1 sends "hello" Client 2 sends "world" (at the same time) Client 1 gets hello world or world hello Client 2 gets the same.I solved the problem by avoiding "private final Set<ChatWebSocket> _members = new CopyOnWriteArraySet<ChatWebSocket>();" in my Servlet-Class and in my Server-Class. Where is that good for?!?! I do not understand it. It works without it, too, and especially correctly.
best regrads hschenk Simone Bordet schrieb:
Hi, On Tue, Jan 10, 2012 at 15:30, Hendrik Schenk <hschenk@xxxxxxx> wrote:hi, I have 2 questions: 1. Is there an opportunity to send a message from server to a specific client which is connected to server?Yes.2. At the moment, i use a servlet fpr the communication between the clients and the server. Is there another way to communicate with the websockets server? I mean, i need a interface where the clients can connect to and send and retrieve messages.See http://wiki.eclipse.org/Jetty/Feature/WebSockets as a starting point. What you need is to extend WebSocketServlet and implement doWebSocketConnect(). That method is invoked for each remote client connection. You should return an implementation of a WebSocket sub-interface, most often WebSocket.OnTextMessage. This object is linked with the remote client and allows you to handle passive events (i.e. events generated by the remote client). To generate events you can use the WebSocket.Connection API. You obtain an instance of this interface in WebSocket.onOpen() and you can use it to send messages and close the connection. See https://github.com/eclipse/jetty.project/blob/master/test-jetty-webapp/src/main/java/com/acme/WebSocketChatServlet.java. Or, you can look at CometD for a simpler way to use WebSocket features: http://cometd.org. Simon
Back to the top