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: