1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-12 11:10:29 +00:00

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
This commit is contained in:
SquidDev 2018-10-23 12:11:03 +01:00
parent ac1f30ef43
commit 822db6e9b5

View File

@ -13,7 +13,9 @@ import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.core.apis.HTTPAPI; import dan200.computercraft.core.apis.HTTPAPI;
import dan200.computercraft.core.apis.IAPIEnvironment; import dan200.computercraft.core.apis.IAPIEnvironment;
import dan200.computercraft.core.tracking.TrackingField; import dan200.computercraft.core.tracking.TrackingField;
import dan200.computercraft.shared.util.StringUtil;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
@ -26,6 +28,8 @@ import javax.annotation.Nullable;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import static dan200.computercraft.core.apis.ArgumentHelper.optBoolean;
public class WebsocketConnection extends SimpleChannelInboundHandler<Object> implements ILuaObject, Closeable public class WebsocketConnection extends SimpleChannelInboundHandler<Object> implements ILuaObject, Closeable
{ {
public static final String SUCCESS_EVENT = "websocket_success"; public static final String SUCCESS_EVENT = "websocket_success";
@ -174,8 +178,11 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
{ {
checkOpen(); checkOpen();
String text = arguments.length > 0 && arguments[0] != null ? arguments[0].toString() : ""; String text = arguments.length > 0 && arguments[0] != null ? arguments[0].toString() : "";
boolean binary = optBoolean(arguments, 1, false);
computer.addTrackingChange( TrackingField.WEBSOCKET_OUTGOING, text.length() ); 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; return null;
} }
case 2: case 2: