gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:28:04 28 4
gpt4 key购买 nike

我正在使用 Eclipse 和 apache 7.x 使用 WebSocket 构建简单的聊天应用程序。但是我无法编译我的代码,因为它显示无法解析导入 javax.websocket。经过大量谷歌搜索后,我发现 Link在应用程序中显示外部 jar。我在我的图书馆部分添加了一个 jar 。它消除了我当前的错误,但给出了另一个错误。现在它显示 与“ws://localhost:8080/WebSocket/socket”的 WebSocket 连接失败:WebSocket 握手期间出错:意外响应代码:404


我已经尝试了很多,例如添加 servlet-api.jar 以从 apache 库构建路径。还可以找到一些信息,例如 它还提供了 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 覆盖了我在 apache tomcat 中的内部 jar。我正在使用没有 websocket-api.jar

的旧 tomcat

所以我对这个问题的解决方案是使用 glashfish 4.x 或使用 apache tomcat 8.x,它为我们提供了运行 websocket 所需的 jar。无需添加任何额外的 websocket api jar。

关于eclipse - WebSocket- "Error during WebSocket handshake: Unexpected response code: 404"将外部 jar 添加到项目时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34148716/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com