我有websocket服务器类,我想在用户连接时进行打印。它可以正常工作,但是,如果我想在此websocket服务器之外执行方法,则它不会打印并关闭客户端的连接,并打印出Java RuntimeException错误。
package com.rupla.myapp.endpoint;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.sql.SQLException;
import javax.websocket.EncodeException;
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;
import com.rupla.myapp.encoders.MessageEncoder;
import com.rupla.myapp.http.MyOtherClas;
import com.rupla.myapp.encoders.MessageDecoder;
@ServerEndpoint(value = "/chatnow", encoders = MessageEncoder.class, decoders = MessageDecoder.class)
public class WebSocketServer {
@OnOpen
public void onOpen(Session session) {
extracted(session);
}
private void extracted(Session session) {
System.out.println("Session Opened (Client to Server) - ID: " + session.getId());
}
@OnMessage
public void handleTextMessage(String message, Session s) throws IOException {
System.out.println("New Text Message Received: " + message + " - From session: " + s.getId());
//So here we send client's message to the client - This works
s.getBasicRemote().sendText("Server: " + message);
try {
// Here I try to print "hello" but from 'MyOtherClas'.class using printTest() method
MyOtherClas.printTest();
//Right after this code, server closes the connection to the client and doesn't print anything
}
catch(Exception e) {
System.out.println("fail: "+e);
}
}
/**
* If I would call this method below, it would work since it's in the same class (but why?)
*/
private void printInsideClas() {
System.out.println("hello");
}
@OnClose
public void onClose(Session session) throws IOException, EncodeException {
System.out.println("Session Closed");
}
@OnError
public void onError(Throwable t) {
System.out.println("onError: " + t);
}
}
用于Java的相当基本的websocket服务器。但是,当我在tomcat 9.0上运行此程序并连接javascript客户端并尝试将消息从客户端发送到服务器时。它关闭服务器和客户端之间的连接,然后打印此错误。
Session Opened (Client to Server) - ID: 4
New Text Message Received: asd - From session: 4
onError: java.lang.RuntimeException: org/apache/http/Header
Session Closed
我暂时尝试用Google搜索几天,但仍然没有答案,我真的需要帮助。
答案 0 :(得分:0)
该异常在服务器端Web服务堆栈中。例外是它无法加载Apache HTTPComponents一部分的核心类。
以下是一些可能的解释:
//your Json data here
string json_object="........";
JavaScriptSerializer jsJson = new JavaScriptSerializer();
jsJson.MaxJsonLength = 2147483644;
MyClass obj = jsJson.Deserialize<MyClass>(json_object);
类依赖于其他一些失败类初始化的类。如果发生了这种情况,则日志文件中应早于证据。您应该查找的JAR文件是“ httpcore-.jar”或旧的Jakarta项目中的等效文件。
找出上面的哪一个发生了,您就可以解决问题了。