初学WebSocket,做了一个Demo,出现404错误。
WebSocket connection to 'ws://localhost:8088/chat/begin?uname=xiaoming' failed: Error during WebSocket handshake: Unexpected response code: 404
JS代码:
var chat={};
chat.socket=null;
var uname="<%=request.getSession().getAttribute("uname")%>"
function conn(){
connect("ws://localhost:8088/chat/begin?uname="+uname);
}
function connect(host){
if("WebSocket" in window){
chat.socket=new WebSocket(host);
}else if("MoWebSocket" in window){
chat.socket=new MoWebSocket(host);
}else{
console.log("当前浏览器不支持WebSocket");
return;
}
}
后台终端代码:
@ServerEndpoint("/begin")
public class ChatServer {
@OnOpen
public void start(Session session){
System.out.println(session.getQueryString());
}
}
使用的是eclipse开发,Web服务器使用Tomcat7,jdk版本是1.8。