尝试使用Java Servlets和tomcat 8.5通过WebSockets更新html / jsp文件中的某些信息以连接到RMI服务器。错误是:
VM281:164 WebSocket connection to 'ws://localhost:8080/sd/ws' failed: Error during WebSocket handshake: Unexpected response code: 200
这是我们的Javascript连接代码:
var websocket;
var elId = null;
window.onload = function() {
connect("ws://"+document.location.host+"/sd/ws")
}
function connect(host) { // connect to the host websocket
if ('WebSocket' in window)
websocket = new WebSocket(host);
else if ('MozWebSocket' in window)
websocket = new MozWebSocket(host);
return;
websocket.onopen = onOpen; // set the event listeners below
websocket.onclose = onClose;
websocket.onmessage = onMessage;
websocket.onerror = onError;
}
我们的Java Servlet代码是这样的:
package ws;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import javax.websocket.server.ServerEndpoint;
import rmi.Election;
import rmi.ServerInterface;
import javax.websocket.OnOpen;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnError;
import javax.websocket.Session;
@ServerEndpoint(value = "/ws")
public class ElectionInfoWebSocket {
private Session session;
private boolean isUp;
private ServerInterface rmiCon;
private String naming = "//127.0.0.1:7000/server";
private String elId = "";
public ElectionInfoWebSocket() {
}
@OnOpen
public void start(Session session) {
this.session = session;
isUp = true;
sendMessage("meme");
}
}
我已将java.websocket jar导入到lib文件中,但它仍无法正常工作。在这里尝试了所有的解决方案没有任何作用,并且URI似乎是正确的,因为我们已经测试了所有可能性。