From 822db6e9b5a4a1992f2e6d11956f5deddf07de6e Mon Sep 17 00:00:00 2001 From: SquidDev Date: Tue, 23 Oct 2018 12:11:03 +0100 Subject: [PATCH] Add support for binary websockets - Add an argument to send which controls whether it's a binary message or not. This is a little ugly, but it's probably more effective than anything else. - Fix binary frames not correctly queueing the correct data in the message event. Closes #69 --- .../core/apis/http/WebsocketConnection.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/dan200/computercraft/core/apis/http/WebsocketConnection.java b/src/main/java/dan200/computercraft/core/apis/http/WebsocketConnection.java index 5efad05b9..f92983a33 100644 --- a/src/main/java/dan200/computercraft/core/apis/http/WebsocketConnection.java +++ b/src/main/java/dan200/computercraft/core/apis/http/WebsocketConnection.java @@ -13,7 +13,9 @@ import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.core.apis.HTTPAPI; import dan200.computercraft.core.apis.IAPIEnvironment; import dan200.computercraft.core.tracking.TrackingField; +import dan200.computercraft.shared.util.StringUtil; import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; @@ -26,6 +28,8 @@ import javax.annotation.Nullable; import java.io.Closeable; import java.io.IOException; +import static dan200.computercraft.core.apis.ArgumentHelper.optBoolean; + public class WebsocketConnection extends SimpleChannelInboundHandler implements ILuaObject, Closeable { public static final String SUCCESS_EVENT = "websocket_success"; @@ -174,8 +178,11 @@ public class WebsocketConnection extends SimpleChannelInboundHandler imp { checkOpen(); String text = arguments.length > 0 && arguments[0] != null ? arguments[0].toString() : ""; + boolean binary = optBoolean(arguments, 1, false); computer.addTrackingChange( TrackingField.WEBSOCKET_OUTGOING, text.length() ); - channel.writeAndFlush( new TextWebSocketFrame( text ) ); + channel.writeAndFlush( binary + ? new BinaryWebSocketFrame( Unpooled.wrappedBuffer( StringUtil.encodeString( text ) ) ) + : new TextWebSocketFrame( text ) ); return null; } case 2: