Java 未调用Jetty WebSockets onBinaryMessage函数
我正在我的应用程序中嵌入Jetty(版本-9.4.1.v20170120)websockets api,并为其实现了以下两个类Java 未调用Jetty WebSockets onBinaryMessage函数,java,websocket,embedded-jetty,Java,Websocket,Embedded Jetty,我正在我的应用程序中嵌入Jetty(版本-9.4.1.v20170120)websockets api,并为其实现了以下两个类 及 但是当我使用android应用程序和AndroidSync发送字节时,onBinaryMessage函数没有被调用。事实上,我甚至使用onFrame方法记录了消息类型,但我只收到“文本”类型的消息,没有收到二进制类型的消息。因此,我得到的是字符串,而不是字节。怎么办???我也检查了其他stackoverflow帖子,但没有一篇有答案?只是在这里使用方法重载让它
及
但是当我使用android应用程序和AndroidSync发送字节时,onBinaryMessage函数没有被调用。事实上,我甚至使用onFrame方法记录了消息类型,但我只收到“文本”类型的消息,没有收到二进制类型的消息。因此,我得到的是字符串,而不是字节。怎么办???我也检查了其他stackoverflow帖子,但没有一篇有答案?只是在这里使用方法重载让它工作了(经过大量搜索后在网上的一些帖子中找到了它……jetty websocket文档和javadoc非常糟糕)
所以现在MyWebSocketHandler.java看起来像
虽然我得到了答案,但我还是想把它贴出来,因为它可能会帮助其他人!!因为我无法通过堆栈溢出找到它
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.websocket.server.WebSocketHandler;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
public class WebSocketTest{
private static final int MAX_MESSAGE_SIZE = 3000000;
public static void main(String[] args) throws Exception {
Server server = new Server(9999);
WebSocketHandler wsHandler = new WebSocketHandler() {
@Override
public void configure(WebSocketServletFactory factory) {
factory.getPolicy().setMaxBinaryMessageSize(MAX_MESSAGE_SIZE);
factory.register(MyWebSocketHandler.class);
}
};
server.setHandler(wsHandler);
server.start();
server.join();
}
}
import org.apache.commons.io.FileUtils;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.*;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import java.io.File;
import java.io.IOException;
@WebSocket
public class MyWebSocketHandler {
private Session session;
@OnWebSocketClose
public void onClose(int statusCode, String reason) {
System.out.println("Close: statusCode=" + statusCode + ", reason=" + reason);
}
@OnWebSocketError
public void onError(Throwable t) {
System.out.println("Error: " + t.getMessage());
}
@OnWebSocketConnect
public void onConnect(Session session) {
this.session = session;
System.out.println("Connect: " + session.getRemoteAddress().getAddress());
try {
session.getRemote().sendString("Hello Webbrowser");
} catch (IOException e) {
e.printStackTrace();
}
}
public void onBinaryMessage(byte[] data, int offset, int length) throws IOException {
System.out.println("Wriiten");
FileUtils.writeByteArrayToFile(new File(System.nanoTime()+"hello.jpg"), data);
try {
this.session.getRemote().sendString("Receiving Binary Data==>"+data);
} catch (IOException e) {
e.printStackTrace();
}
}
@OnWebSocketMessage
public void onMessage(String message) {
System.out.println("String Message ====> " + message);
try {
this.session.getRemote().sendString("Receiving String Data==>"+message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@OnWebSocketMessage
public void onMessage(byte[] data, int offset, int length) throws IOException {
System.out.println("Wriiten");
FileUtils.writeByteArrayToFile(new File(System.nanoTime()+"hello.jpg"), data);
try {
this.session.getRemote().sendString("Receiving Binary Data==>"+data);
} catch (IOException e) {
e.printStackTrace();
}
}
import org.apache.commons.io.FileUtils;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.*;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import java.io.File;
import java.io.IOException;
@WebSocket
public class MyWebSocketHandler {
private Session session;
@OnWebSocketClose
public void onClose(int statusCode, String reason) {
System.out.println("Close: statusCode=" + statusCode + ", reason=" + reason);
}
@OnWebSocketError
public void onError(Throwable t) {
System.out.println("Error: " + t.getMessage());
}
@OnWebSocketConnect
public void onConnect(Session session) {
this.session = session;
System.out.println("Connect: " + session.getRemoteAddress().getAddress());
try {
session.getRemote().sendString("Hello Webbrowser");
} catch (IOException e) {
e.printStackTrace();
}
}
@OnWebSocketMessage
public void onMessage(byte[] data, int offset, int length) throws IOException {
System.out.println("Wriiten");
FileUtils.writeByteArrayToFile(new File(System.nanoTime()+"hello.jpg"), data);
try {
this.session.getRemote().sendString("Receiving Binary Data==>"+data);
} catch (IOException e) {
e.printStackTrace();
}
}
@OnWebSocketMessage
public void onMessage(String message) {
System.out.println("String Message ====> " + message);
try {
this.session.getRemote().sendString("Receiving String Data==>"+message);
} catch (IOException e) {
e.printStackTrace();
}
}
}