这是我的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");
}
}
这是我的websocket客户端:
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);
}
我在jsf模板中编写了这段代码,因此我的JSF应用程序中的所有页面都运行它。
当我尝试连接时,我收到以下错误:
与'ws:// localhost:8080 / chp / mediador'的WebSocket连接失败:WebSocket握手期间出错:意外响应代码:404
答案 0 :(得分:1)
有时事情会单独解决......
也许是图书馆问题。我做了一个干净然后部署,现在它正在运行。
谢谢!