Hi,
So once I have resolved some of the maven issues I typed up a very simple echo example with the service:
package websocket;
import java.io.IOException;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint(value="/echo")
public class EchoBean
{
@OnMessage
public void echo (String message, Session peer) throws IOException {
//
System.out.println("Echoing " + message);
System.out.println("Sending message to client");
peer.getBasicRemote().sendText(message);
}
@OnOpen
public void onOpen(final Session session, EndpointConfig endpointConfig) {
System.out.println("Server connected " + session + " " + endpointConfig);
}
}
And the client, which interestedly wouldn't work if it was just a annotated POJO - it had to extend Endpoint on the client side:
package websocket;
import java.io.IOException;
import javax.websocket.ClientEndpoint;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
@ClientEndpoint
public class EchoBeanClient
extends Endpoint
{
public void onOpen(Session session, EndpointConfig endpointConfig) {
System.out.println("Client Connection open " + session + " " + endpointConfig);
// Add a listener
session.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String string) {
System.out.println("Message from server : " + string);
}
});
System.out.println("Sending message to server");
session.getAsyncRemote().sendText("Hello");
}
}
And finally the test method:
public static void main(String[] args) throws DeploymentException, IOException {
try
{
Server server = new Server("localhost", 8025, "/", EchoBean.class);
server.start();
// .start() doesn't mean server is ready to serve
TimeUnit.SECONDS.sleep(5);
// Right now we have to create a client
ClientManager client = ClientManager.createClient();
Session session = client.connectToServer(
EchoBeanClient.class,
ClientEndpointConfig.Builder.create().build(),
URI.create("ws://localhost:8025/echo"));
session.getBasicRemote().sendText("Hello");
//
TimeUnit.SECONDS.sleep(5);
}
catch (Exception ex) {
ex.printStackTrace(System.out);
}
The problem is that the server gets the echo message twice, see the following output:
Apr 29, 2013 3:34:20 PM org.glassfish.tyrus.server.ServerContainerFactory create
INFO: Provider class loaded: org.glassfish.tyrus.container.grizzly.GrizzlyEngine
Apr 29, 2013 3:34:20 PM org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [0.0.0.0:8025]
Apr 29, 2013 3:34:20 PM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
Apr 29, 2013 3:34:20 PM org.glassfish.tyrus.server.Server start
INFO: WebSocket Registered apps: URLs all start with ws://localhost:8025
Apr 29, 2013 3:34:20 PM org.glassfish.tyrus.server.Server start
INFO: WebSocket server started.
Server connected SessionImpl{uri=/echo, id='3ceec83b-3c9e-4889-8d0e-0d8dfbb6839f', endpoint=EndpointWrapper{endpointClass=null, endpoint=org.glassfish.tyrus.core.AnnotatedEndpoint_at_328f00, uri='/echo', contextPath='/'}} javax.websocket.server.DefaultServerEndpointConfig_at_14a5255
Client Connection open SessionImpl{uri=ws://localhost:8025/echo, id='fd1b240f-f59c-4a59-a1a9-77b2762cfa1b', endpoint=EndpointWrapper{endpointClass=null, endpoint=websocket.EchoBeanClient_at_ec120d, uri='ws://localhost:8025/echo', contextPath='ws://localhost:8025/echo'}} javax.websocket.DefaultClientEndpointConfig_at_954f73
Sending message to server
Echoing Hello
Sending message to client
Echoing Hello
Sending message to client
Message from server : Hello
Message from server : Hello
Is there something obvious missing from this code?
Thanks,
Gerard