mardi 19 mai 2015

Error 404 when connecting to websocket

This is my ServerEndpoint:

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/mediador")
public class Mediador {

    private final static Logger LOGGER = Logger.getLogger(Mediador.class.getName());

    @OnOpen
    public void onOpen(Throwable t) {
        LOGGER.log(Level.INFO, "Método Mediador.onOpen");
    }

    @OnError
    public void onError(Session aclientSession, Throwable aThrowable) {
        System.out.println("Error : " + aclientSession);
        System.out.println("Error :" + aThrowable);
    }

    @OnMessage
    public void onMessage(Throwable t) {
        LOGGER.log(Level.INFO, "Método Mediador.onMessage");
    }

    @OnClose
    public void onClose(Throwable t) {
        LOGGER.log(Level.INFO, "Método Mediador.onClose");
    }

}

And this is my websocket client:

var ws = new WebSocket("ws://" + document.location.host + "/chp/mediador");
// also tried this
// var ws = new WebSocket("ws://" + document.location.host + "/mediador");
console.log(ws);

ws.onerror = function (event) {
    console.log("error " + ws);
};

ws.onopen = function (evento) {
    console.log("abierto");
    ws.send("hola");
}

ws.onmessage = function (evento) {
    console.log(evento.data);
}

I wrote this code in my jsf template so all my pages in my JSF application run it.

When I try to connect I get the following error:

WebSocket connection to 'ws://localhost:8080/chp/mediador' failed: Error during WebSocket handshake: Unexpected response code: 404

Aucun commentaire:

Enregistrer un commentaire