Connecting websocket to local application through the application endpoint

时间:2018-07-24 10:09:13

标签: java websocket connection endpoint java-websocket

I'm trying to create a websocket in java that listens to a local application on the endpoint f.ex. "wss://localhost.localapp.com:8080/".

The application do send through that websocket information about what is happening.

I do have a HTML file and a JavaScript file.

In javascript it is really easy to create the connection with a websocket and get messages from the application.

But in java, I don't get anything through the same websocket. I wonder is there anything I'm missing?

The java file isn't connected to my HTML file in anyway. Do it have to be? I have seen several examples of where the endpoint is almost the same as the url of the webpage, but I only want to connect to the applications endpoint and get information from there.

@ClientEndpoint
public class ClientEndpoint {

    private Logger logger = Logger.getLogger(getClass().getName());

    public static void main(String[] args) {
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        try {
            container.connectToServer(Client.class, new URI("wss://localhost.localapp.com:8080/"));
        } catch (DeploymentException | URISyntaxException | InterruptedException | IOException e) {
            System.out.println("Connection error occured!: " + e);
        }
    }

    @OnOpen
    public void onOpen(Session session) {
        this.logger.info("New websocket session opened: " + session.getId());
    }

    @OnClose
    public void onClose(Session session) {
        this.logger.info("Websoket session closed: " + session.getId());
    }

    @OnMessage
    public void onMessage(Session session, String message) throws IOException, EncodeException {
        this.logger.info("Message recieved: " + message);
    }

    @OnError
    public void error(Session session, Throwable t) {
        t.printStackTrace();
    }
}

What else must be done to get the connection to the local application through the websocket?

UPDATE:

I got it to work. I needed to be connected to a server and create a new instance of the ClientEndpoint class in a java file that I knew that runned.

UPDATE2:

This probably cannot be done because the application is on your local computer and the server is not local.

But could this be done if you would use a local server?

0 个答案:

没有答案