0

I'm currently testing Websockets with Glassfish 4 and the "on board" implementation but cannot send or receive any messages although following tutorials like The Java EE 7 Tutorial - Sending and Receiving Messages or How to build Java WebSocket Applications Using the JSR 356 API. I want to implement a simple client server communication so all I have is a

server:

@ServerEndpoint("/echotest")
public class Server {
    private static final Logger LOGGER = LoggerFactory.getLogger(Server.class);

    @OnOpen
    public void onOpen(Session session) {
        LOGGER.info("OnOpen invoked by Session '{}'.", session.getId());

        try {
            session.getBasicRemote().sendText("Hello Client!");
        } catch (IOException ex) {
            LOGGER.error("Message delivery failed.", ex);
        }
    }

    @OnClose
    public void onClose(Session session, CloseReason closeReason) {
        LOGGER.info("OnClose invoked by Session '{}'; Reason: {}.", session.getId(), closeReason.getReasonPhrase());
    }

    @OnMessage
    public void onMessage(Session session, String msg) {
        LOGGER.info("OnMessage invoked by Session '{}'; Message: {}.", session.getId(), msg);
    }
}

and a client:

@ClientEndpoint
public class Client {

    private static CountDownLatch latch;
    private static final Logger LOGGER = LoggerFactory.getLogger(Client.class);

    @OnOpen
    public void onOpen(Session session) {
        LOGGER.info("OnOpen invoked by Session '{}'.", session.getId());

        try {
            session.getBasicRemote().sendText("Hello Server!");
        } catch (IOException ex) {
            LOGGER.error("Message delivery failed.", ex);
        }
    }

    @OnClose
    public void onClose(Session session, CloseReason closeReason) {
        LOGGER.info("OnClose invoked by Session '{}'; Reason: {}.", session.getId(), closeReason.getReasonPhrase());
    }

    @OnMessage
    public void onMessage(Session session, String msg) {
        LOGGER.info("OnMessage invoked by Session '{}'; Message: {}.", session.getId(), msg);
    }

    public static void main(String[] args) {
        latch = new CountDownLatch(1);

        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        try {
            container.connectToServer(Client.class, new URI("ws://localhost:8080/Tstr/echotest"));
            latch.await();

        } catch (DeploymentException | URISyntaxException | InterruptedException | IOException e) {
            LOGGER.error("Connection error occured!", e);
        }
    }
}

The Websocket is available and I can connect to the server (changing the URI leads to a 404 handsheak error) but none of the (server sided) methods will be invoked and the only log message I'm getting is:

INFO: OnOpen invoked by Session 'acc92925-6682-4414-9e78-cf60a624b014'.

on the client side. I would expect the server to at least log the onOpen invokation. Using an (untouched) Glassfish 4 with Netbeans 8 for this test.

Any suggestions why there are no messages exchanged?

Patrick
  • 55
  • 1
  • 7

1 Answers1

0

Your code works for me, however I had to replace Logger (slf4j?) with this one java.util.logging.Logger

  • thanks for the quick help! kind of odd that no server sided method is invoked when Glassfish is used with SLF4J and no other exceptions are thrown ... again, thank you very much – Patrick Jun 18 '14 at 11:46