我在我的应用程序中嵌入了Jetty(版本 - 9.4.1.v20170120)websockets api,并为它实现了以下两个类
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();
}
}
}
但是当我使用带有AndroidAsync的Android应用程序发送字节时,onBinaryMessage函数没有被调用,实际上我甚至使用onFrame方法记录了Message类型,但我收到的消息只有' TEXT'类型,没有二进制类型。所以我得到字符串,但不是字节。怎么办???我还检查了其他stackoverflow帖子,但没有人有答案?
答案 0 :(得分:1)
刚刚在这里使用方法重载工作(经过大量搜索后在网上找到了一些帖子... jetty websocket文档和javadoc非常糟糕)
@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();
}
}
所以现在MyWebSocketHandler.java看起来像
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();
}
}
}
虽然我得到了答案,但我仍然想发布它,因为它可能会帮助别人!因为我无法通过堆栈溢出找到它