WebSocket - 将外部 jar 添加到项目时"Error during WebSocket handshake: Unexpected response code: 404"



我正在使用Eclipse和apache7.x使用WebSocket构建简单的聊天应用程序。但是我无法编译我的代码,因为它显示导入javax.WebSocket无法解析。在谷歌上搜索了很多之后,我发现了在应用程序中显示外部jar的链接。我在我的图书馆部分添加了一个罐子。它删除了我当前的错误,但给出了另一个错误。现在它显示WebSocket连接到"ws://localhost:8080/Webocket/socket"失败:WebSocket握手期间出错:意外响应代码:404


我尝试了很多方法,比如添加servlet-api.jar来从apachelib构建路径。还可以找到一些信息,如它还提供了javax.websocket-api-1.0.jar库,因此应用程序中不需要它。您可能在编译时需要它,但服务器将在运行时提供它

现在我陷入了递归的困境。当我删除我的jar时,出现了第一个错误,当我添加外部jar时,又出现了第二个错误。请在这个问题上帮助我,因为我们正在开发实时协作编辑器的迷你项目,我们应该在其中使用websocket。


下面是我的websocket代码(不使用maven):

WebSocket.java

package com.psl.service;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;

@ServerEndpoint("/socket")
public class WebSocket {
Set<Session> sessionSet = Collections.synchronizedSet(new HashSet<>());
@OnOpen
public void onOpen(Session webSession) {
    System.out.println("Opened");
    sessionSet.add(webSession);
}
@OnMessage
public void onMessage(String message, Session webSession) throws IOException {
    System.out.println("Got message    "+message);
    String username = (String)webSession.getUserProperties().get("username");
    if(username==null) {
        webSession.getUserProperties().put("username", message);
        webSession.getBasicRemote().sendText(buildJSON(username, message));
    }
    else {
        for(Session s : sessionSet) {
            s.getBasicRemote().sendText(buildJSON(username, message));
        }
    }
}

private String buildJSON(String string, String string2) {
    String jsonData="";
    JSONObject obj=new JSONObject();
      try {
        obj.put("message",string + ":" + string2);
        StringWriter out = new StringWriter();
          JSONWriter jwriter = new JSONWriter(out);
          jwriter.key(string).value(string2);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
      return null;
}
@OnClose
public void onClose() {
    System.out.println("CLosed");
    sessionSet.remove(sessionSet);
}
}

chat.html

    <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
    var websocket = new WebSocket("ws://localhost:8080/WebSocketDemo/socket");
    websocket.onmessage = function onMessage(message) {
        var jsonData = JSON.parse(message.data);
        if(jsonData.message != null) {
            messageTextArea.value += jsonData.message + "n";
        }
    }
    function sendMessage() {
        websocket.send(sendText.value);
        sendText.value = "";
    }
</script>
</head>
<body>
<textarea rows="30" cols="70" id="messageTextArea" ></textarea><br /><br />
<input id="sendText">
<input type="button" value="Send" onclick="sendMessage()">

</body>
</html>

我得到了这个查询的答案。这是因为添加的jar覆盖了我在apachetomcat中的内部jar。我使用的是没有websocket-api.jar的旧tomcat

因此,我对这个问题的解决方案是要么使用glashfish4.x,要么使用apachetomcat8.x,它为我们提供了运行websocket所需的jar。无需添加任何额外的websocketapi jar。

最新更新