通过 WebSocket (BinaryWebSocketFrame) 和 Netty 上传文件
Upload file via WebSocket (BinaryWebSocketFrame) and Netty
我正在尝试使用以下代码通过 websockets 上传文件:
let file = e.target.files[0]
let ws = new WebSocket("ws://127.0.0.1:8080/")
ws.binaryType = "arraybuffer";
var reader = new FileReader();
var rawData = new ArrayBuffer();
ws.onopen = () => {
reader.loadend = function() {}
reader.onload = function(e) {
rawData = e.target.result;
ws.send(rawData);
console.log("Transfer complete.")
}
reader.readAsArrayBuffer(file);
}
websocket 可以很好地处理文本,连接绝对不是问题。但是在非常详细 netty 文档中,我找不到任何如何处理收到的 BinaryWebSocketFrame 的信息。这里是 java 代码:
public class SocketHandler extends SimpleChannelInboundHandler<Object> {
@Override
public void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof WebSocketFrame) {
if (msg instanceof BinaryWebSocketFrame) {
System.out.println("BinaryWebSocketFrame Received : ");
BinaryWebSocketFrame frame = (BinaryWebSocketFrame) msg;
//This is nothing more than an echo.. It is received by the client successfully.
ctx.writeAndFlush(new BinaryWebSocketFrame(frame.isFinalFragment(), frame.rsv(), frame.content()));
} else if (msg instanceof TextWebSocketFrame) {
System.out.println("TextWebSocketFrame Received : " + ((TextWebSocketFrame) msg).text());
}
}
}
}
我的问题是 BinaryWebSocketFrame.. 我现在如何将它存储到文件中?或者这个bytebuf里面包含了什么?我可以存储文件应该存储在 buf 中的路径吗?
我不擅长 ByteBuffers,所以请详细说明。.netty 中没有关于这件事的文档。
BinaryWebSocketFrame.content()
方法returns一个ByteBuf
。 ByteBuf 方法有一个 readBytes(...)
方法,它接受一个 OutputStream
,你可以用它来写入文件。
我找到了...这里还有一个代码示例
BinaryWebSocketFrame frame = (BinaryWebSocketFrame) msg;
ByteBuf buffer = frame.content().retain();
FileOutputStream outputStream = new FileOutputStream(new File("test.png"));
try {
if (buffer.isReadable()) {
buffer.readBytes(outputStream, buffer.readableBytes());
}
} finally {
outputStream.close();
}
我正在尝试使用以下代码通过 websockets 上传文件:
let file = e.target.files[0]
let ws = new WebSocket("ws://127.0.0.1:8080/")
ws.binaryType = "arraybuffer";
var reader = new FileReader();
var rawData = new ArrayBuffer();
ws.onopen = () => {
reader.loadend = function() {}
reader.onload = function(e) {
rawData = e.target.result;
ws.send(rawData);
console.log("Transfer complete.")
}
reader.readAsArrayBuffer(file);
}
websocket 可以很好地处理文本,连接绝对不是问题。但是在非常详细 netty 文档中,我找不到任何如何处理收到的 BinaryWebSocketFrame 的信息。这里是 java 代码:
public class SocketHandler extends SimpleChannelInboundHandler<Object> {
@Override
public void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof WebSocketFrame) {
if (msg instanceof BinaryWebSocketFrame) {
System.out.println("BinaryWebSocketFrame Received : ");
BinaryWebSocketFrame frame = (BinaryWebSocketFrame) msg;
//This is nothing more than an echo.. It is received by the client successfully.
ctx.writeAndFlush(new BinaryWebSocketFrame(frame.isFinalFragment(), frame.rsv(), frame.content()));
} else if (msg instanceof TextWebSocketFrame) {
System.out.println("TextWebSocketFrame Received : " + ((TextWebSocketFrame) msg).text());
}
}
}
}
我的问题是 BinaryWebSocketFrame.. 我现在如何将它存储到文件中?或者这个bytebuf里面包含了什么?我可以存储文件应该存储在 buf 中的路径吗? 我不擅长 ByteBuffers,所以请详细说明。.netty 中没有关于这件事的文档。
BinaryWebSocketFrame.content()
方法returns一个ByteBuf
。 ByteBuf 方法有一个 readBytes(...)
方法,它接受一个 OutputStream
,你可以用它来写入文件。
我找到了...这里还有一个代码示例
BinaryWebSocketFrame frame = (BinaryWebSocketFrame) msg;
ByteBuf buffer = frame.content().retain();
FileOutputStream outputStream = new FileOutputStream(new File("test.png"));
try {
if (buffer.isReadable()) {
buffer.readBytes(outputStream, buffer.readableBytes());
}
} finally {
outputStream.close();
}