mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-12-12 03:00:30 +00:00
A couple of enhancements to websockets
- Provide whether a message was binary or text in websocket_message and handle.receive(). (Fixes #96) - Provide an optional reason and status code within the websocket_close event. Off topic, but also cleanup the file handles a little.
This commit is contained in:
parent
34d43d8273
commit
744bba300e
@ -888,6 +888,18 @@ public class ComputerCraft
|
||||
return TurtleUpgrades.get( stack );
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static IPocketUpgrade getPocketUpgrade( String id )
|
||||
{
|
||||
return dan200.computercraft.shared.PocketUpgrades.get( id );
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static ITurtleUpgrade getTurtleUpgrade( String id )
|
||||
{
|
||||
return TurtleUpgrades.get( id );
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static IPeripheral getPeripheralAt( World world, BlockPos pos, EnumFacing side )
|
||||
{
|
||||
|
@ -59,8 +59,7 @@ public class BinaryReadableHandle extends HandleGeneric
|
||||
{
|
||||
switch( method )
|
||||
{
|
||||
case 0:
|
||||
// read
|
||||
case 0: // read
|
||||
checkOpen();
|
||||
try
|
||||
{
|
||||
@ -134,8 +133,7 @@ public class BinaryReadableHandle extends HandleGeneric
|
||||
{
|
||||
return null;
|
||||
}
|
||||
case 1:
|
||||
// readAll
|
||||
case 1: // readAll
|
||||
checkOpen();
|
||||
try
|
||||
{
|
||||
@ -163,9 +161,8 @@ public class BinaryReadableHandle extends HandleGeneric
|
||||
{
|
||||
return null;
|
||||
}
|
||||
case 2:
|
||||
case 2: // readLine
|
||||
{
|
||||
// readLine
|
||||
checkOpen();
|
||||
boolean withTrailing = optBoolean( args, 0, false );
|
||||
try
|
||||
@ -199,12 +196,10 @@ public class BinaryReadableHandle extends HandleGeneric
|
||||
return null;
|
||||
}
|
||||
}
|
||||
case 3:
|
||||
//close
|
||||
case 3: // close
|
||||
close();
|
||||
return null;
|
||||
case 4:
|
||||
// seek
|
||||
case 4: // seek
|
||||
checkOpen();
|
||||
return handleSeek( m_seekable, args );
|
||||
default:
|
||||
|
@ -53,8 +53,7 @@ public class BinaryWritableHandle extends HandleGeneric
|
||||
{
|
||||
switch( method )
|
||||
{
|
||||
case 0:
|
||||
// write
|
||||
case 0: // write
|
||||
checkOpen();
|
||||
try
|
||||
{
|
||||
@ -82,8 +81,7 @@ public class BinaryWritableHandle extends HandleGeneric
|
||||
{
|
||||
throw new LuaException( e.getMessage() );
|
||||
}
|
||||
case 1:
|
||||
// flush
|
||||
case 1: // flush
|
||||
checkOpen();
|
||||
try
|
||||
{
|
||||
@ -96,12 +94,10 @@ public class BinaryWritableHandle extends HandleGeneric
|
||||
{
|
||||
return null;
|
||||
}
|
||||
case 2:
|
||||
//close
|
||||
case 2: // close
|
||||
close();
|
||||
return null;
|
||||
case 3:
|
||||
// seek
|
||||
case 3: // seek
|
||||
checkOpen();
|
||||
return handleSeek( m_seekable, args );
|
||||
default:
|
||||
|
@ -47,8 +47,8 @@ public class EncodedReadableHandle extends HandleGeneric
|
||||
return new String[] {
|
||||
"readLine",
|
||||
"readAll",
|
||||
"close",
|
||||
"read",
|
||||
"close",
|
||||
};
|
||||
}
|
||||
|
||||
@ -57,9 +57,8 @@ public class EncodedReadableHandle extends HandleGeneric
|
||||
{
|
||||
switch( method )
|
||||
{
|
||||
case 0:
|
||||
case 0: // readLine
|
||||
{
|
||||
// readLine
|
||||
checkOpen();
|
||||
boolean withTrailing = optBoolean( args, 0, false );
|
||||
try
|
||||
@ -81,8 +80,7 @@ public class EncodedReadableHandle extends HandleGeneric
|
||||
return null;
|
||||
}
|
||||
}
|
||||
case 1:
|
||||
// readAll
|
||||
case 1: // readAll
|
||||
checkOpen();
|
||||
try
|
||||
{
|
||||
@ -103,12 +101,7 @@ public class EncodedReadableHandle extends HandleGeneric
|
||||
{
|
||||
return null;
|
||||
}
|
||||
case 2:
|
||||
// close
|
||||
close();
|
||||
return null;
|
||||
case 3:
|
||||
// read
|
||||
case 2: // read
|
||||
checkOpen();
|
||||
try
|
||||
{
|
||||
@ -158,6 +151,9 @@ public class EncodedReadableHandle extends HandleGeneric
|
||||
{
|
||||
return null;
|
||||
}
|
||||
case 3: // close
|
||||
close();
|
||||
return null;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
@ -52,19 +52,10 @@ public class EncodedWritableHandle extends HandleGeneric
|
||||
{
|
||||
switch( method )
|
||||
{
|
||||
case 0:
|
||||
case 0: // write
|
||||
{
|
||||
// write
|
||||
checkOpen();
|
||||
String text;
|
||||
if( args.length > 0 && args[0] != null )
|
||||
{
|
||||
text = args[0].toString();
|
||||
}
|
||||
else
|
||||
{
|
||||
text = "";
|
||||
}
|
||||
String text = args.length > 0 && args[0] != null ? args[0].toString() : "";
|
||||
try
|
||||
{
|
||||
m_writer.write( text, 0, text.length() );
|
||||
@ -75,19 +66,10 @@ public class EncodedWritableHandle extends HandleGeneric
|
||||
throw new LuaException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
case 1:
|
||||
case 1: // writeLine
|
||||
{
|
||||
// writeLine
|
||||
checkOpen();
|
||||
String text;
|
||||
if( args.length > 0 && args[0] != null )
|
||||
{
|
||||
text = args[0].toString();
|
||||
}
|
||||
else
|
||||
{
|
||||
text = "";
|
||||
}
|
||||
String text = args.length > 0 && args[0] != null ? args[0].toString() : "";
|
||||
try
|
||||
{
|
||||
m_writer.write( text, 0, text.length() );
|
||||
@ -99,8 +81,7 @@ public class EncodedWritableHandle extends HandleGeneric
|
||||
throw new LuaException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
case 2:
|
||||
// flush
|
||||
case 2: // flush
|
||||
checkOpen();
|
||||
try
|
||||
{
|
||||
@ -111,8 +92,7 @@ public class EncodedWritableHandle extends HandleGeneric
|
||||
{
|
||||
return null;
|
||||
}
|
||||
case 3:
|
||||
// close
|
||||
case 3: // close
|
||||
close();
|
||||
return null;
|
||||
default:
|
||||
|
@ -7,6 +7,7 @@
|
||||
package dan200.computercraft.core.apis.http;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.base.Strings;
|
||||
import dan200.computercraft.api.lua.ILuaContext;
|
||||
import dan200.computercraft.api.lua.ILuaObject;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
@ -26,7 +27,7 @@ import io.netty.util.CharsetUtil;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.optBoolean;
|
||||
|
||||
@ -68,15 +69,20 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
public void close()
|
||||
{
|
||||
close( false );
|
||||
}
|
||||
|
||||
private void onClosed()
|
||||
private void onClosed( int status, String reason )
|
||||
{
|
||||
close( true );
|
||||
computer.queueEvent( CLOSE_EVENT, new Object[] { url } );
|
||||
|
||||
computer.queueEvent( CLOSE_EVENT, new Object[] {
|
||||
url,
|
||||
Strings.isNullOrEmpty( reason ) ? null : reason,
|
||||
status < 0 ? null : status,
|
||||
} );
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -96,12 +102,12 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
||||
@Override
|
||||
public void channelInactive( ChannelHandlerContext ctx ) throws Exception
|
||||
{
|
||||
onClosed();
|
||||
onClosed( -1, "Websocket is inactive" );
|
||||
super.channelInactive( ctx );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelRead0( ChannelHandlerContext ctx, Object msg ) throws Exception
|
||||
public void channelRead0( ChannelHandlerContext ctx, Object msg )
|
||||
{
|
||||
Channel ch = ctx.channel();
|
||||
if( !handshaker.isHandshakeComplete() )
|
||||
@ -123,7 +129,7 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
||||
String data = ((TextWebSocketFrame) frame).text();
|
||||
|
||||
computer.addTrackingChange( TrackingField.WEBSOCKET_INCOMING, data.length() );
|
||||
computer.queueEvent( MESSAGE_EVENT, new Object[] { url, data } );
|
||||
computer.queueEvent( MESSAGE_EVENT, new Object[] { url, data, false } );
|
||||
}
|
||||
else if( frame instanceof BinaryWebSocketFrame )
|
||||
{
|
||||
@ -132,12 +138,13 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
||||
data.readBytes( converted );
|
||||
|
||||
computer.addTrackingChange( TrackingField.WEBSOCKET_INCOMING, converted.length );
|
||||
computer.queueEvent( MESSAGE_EVENT, new Object[] { url, converted } );
|
||||
computer.queueEvent( MESSAGE_EVENT, new Object[] { url, converted, true } );
|
||||
}
|
||||
else if( frame instanceof CloseWebSocketFrame )
|
||||
{
|
||||
CloseWebSocketFrame closeFrame = (CloseWebSocketFrame) frame;
|
||||
ch.close();
|
||||
onClosed();
|
||||
onClosed( closeFrame.statusCode(), closeFrame.reasonText() );
|
||||
}
|
||||
}
|
||||
|
||||
@ -164,17 +171,17 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
||||
{
|
||||
switch( method )
|
||||
{
|
||||
case 0:
|
||||
case 0: // receive
|
||||
while( true )
|
||||
{
|
||||
checkOpen();
|
||||
Object[] event = context.pullEvent( MESSAGE_EVENT );
|
||||
if( event.length >= 3 && Objects.equal( event[1], url ) )
|
||||
{
|
||||
return new Object[] { event[2] };
|
||||
return Arrays.copyOfRange( event, 2, event.length );
|
||||
}
|
||||
}
|
||||
case 1:
|
||||
case 1: // send
|
||||
{
|
||||
checkOpen();
|
||||
String text = arguments.length > 0 && arguments[0] != null ? arguments[0].toString() : "";
|
||||
|
Loading…
Reference in New Issue
Block a user