mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-11-04 17:16:20 +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 );
|
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
|
@Deprecated
|
||||||
public static IPeripheral getPeripheralAt( World world, BlockPos pos, EnumFacing side )
|
public static IPeripheral getPeripheralAt( World world, BlockPos pos, EnumFacing side )
|
||||||
{
|
{
|
||||||
|
@ -59,8 +59,7 @@ public class BinaryReadableHandle extends HandleGeneric
|
|||||||
{
|
{
|
||||||
switch( method )
|
switch( method )
|
||||||
{
|
{
|
||||||
case 0:
|
case 0: // read
|
||||||
// read
|
|
||||||
checkOpen();
|
checkOpen();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -134,8 +133,7 @@ public class BinaryReadableHandle extends HandleGeneric
|
|||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
case 1:
|
case 1: // readAll
|
||||||
// readAll
|
|
||||||
checkOpen();
|
checkOpen();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -163,9 +161,8 @@ public class BinaryReadableHandle extends HandleGeneric
|
|||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
case 2:
|
case 2: // readLine
|
||||||
{
|
{
|
||||||
// readLine
|
|
||||||
checkOpen();
|
checkOpen();
|
||||||
boolean withTrailing = optBoolean( args, 0, false );
|
boolean withTrailing = optBoolean( args, 0, false );
|
||||||
try
|
try
|
||||||
@ -199,12 +196,10 @@ public class BinaryReadableHandle extends HandleGeneric
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case 3:
|
case 3: // close
|
||||||
//close
|
|
||||||
close();
|
close();
|
||||||
return null;
|
return null;
|
||||||
case 4:
|
case 4: // seek
|
||||||
// seek
|
|
||||||
checkOpen();
|
checkOpen();
|
||||||
return handleSeek( m_seekable, args );
|
return handleSeek( m_seekable, args );
|
||||||
default:
|
default:
|
||||||
|
@ -53,8 +53,7 @@ public class BinaryWritableHandle extends HandleGeneric
|
|||||||
{
|
{
|
||||||
switch( method )
|
switch( method )
|
||||||
{
|
{
|
||||||
case 0:
|
case 0: // write
|
||||||
// write
|
|
||||||
checkOpen();
|
checkOpen();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -82,8 +81,7 @@ public class BinaryWritableHandle extends HandleGeneric
|
|||||||
{
|
{
|
||||||
throw new LuaException( e.getMessage() );
|
throw new LuaException( e.getMessage() );
|
||||||
}
|
}
|
||||||
case 1:
|
case 1: // flush
|
||||||
// flush
|
|
||||||
checkOpen();
|
checkOpen();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -96,12 +94,10 @@ public class BinaryWritableHandle extends HandleGeneric
|
|||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
case 2:
|
case 2: // close
|
||||||
//close
|
|
||||||
close();
|
close();
|
||||||
return null;
|
return null;
|
||||||
case 3:
|
case 3: // seek
|
||||||
// seek
|
|
||||||
checkOpen();
|
checkOpen();
|
||||||
return handleSeek( m_seekable, args );
|
return handleSeek( m_seekable, args );
|
||||||
default:
|
default:
|
||||||
|
@ -47,8 +47,8 @@ public class EncodedReadableHandle extends HandleGeneric
|
|||||||
return new String[] {
|
return new String[] {
|
||||||
"readLine",
|
"readLine",
|
||||||
"readAll",
|
"readAll",
|
||||||
"close",
|
|
||||||
"read",
|
"read",
|
||||||
|
"close",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,9 +57,8 @@ public class EncodedReadableHandle extends HandleGeneric
|
|||||||
{
|
{
|
||||||
switch( method )
|
switch( method )
|
||||||
{
|
{
|
||||||
case 0:
|
case 0: // readLine
|
||||||
{
|
{
|
||||||
// readLine
|
|
||||||
checkOpen();
|
checkOpen();
|
||||||
boolean withTrailing = optBoolean( args, 0, false );
|
boolean withTrailing = optBoolean( args, 0, false );
|
||||||
try
|
try
|
||||||
@ -81,8 +80,7 @@ public class EncodedReadableHandle extends HandleGeneric
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case 1:
|
case 1: // readAll
|
||||||
// readAll
|
|
||||||
checkOpen();
|
checkOpen();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -103,12 +101,7 @@ public class EncodedReadableHandle extends HandleGeneric
|
|||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
case 2:
|
case 2: // read
|
||||||
// close
|
|
||||||
close();
|
|
||||||
return null;
|
|
||||||
case 3:
|
|
||||||
// read
|
|
||||||
checkOpen();
|
checkOpen();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -158,6 +151,9 @@ public class EncodedReadableHandle extends HandleGeneric
|
|||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
case 3: // close
|
||||||
|
close();
|
||||||
|
return null;
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -52,19 +52,10 @@ public class EncodedWritableHandle extends HandleGeneric
|
|||||||
{
|
{
|
||||||
switch( method )
|
switch( method )
|
||||||
{
|
{
|
||||||
case 0:
|
case 0: // write
|
||||||
{
|
{
|
||||||
// write
|
|
||||||
checkOpen();
|
checkOpen();
|
||||||
String text;
|
String text = args.length > 0 && args[0] != null ? args[0].toString() : "";
|
||||||
if( args.length > 0 && args[0] != null )
|
|
||||||
{
|
|
||||||
text = args[0].toString();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
text = "";
|
|
||||||
}
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
m_writer.write( text, 0, text.length() );
|
m_writer.write( text, 0, text.length() );
|
||||||
@ -75,19 +66,10 @@ public class EncodedWritableHandle extends HandleGeneric
|
|||||||
throw new LuaException( e.getMessage() );
|
throw new LuaException( e.getMessage() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case 1:
|
case 1: // writeLine
|
||||||
{
|
{
|
||||||
// writeLine
|
|
||||||
checkOpen();
|
checkOpen();
|
||||||
String text;
|
String text = args.length > 0 && args[0] != null ? args[0].toString() : "";
|
||||||
if( args.length > 0 && args[0] != null )
|
|
||||||
{
|
|
||||||
text = args[0].toString();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
text = "";
|
|
||||||
}
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
m_writer.write( text, 0, text.length() );
|
m_writer.write( text, 0, text.length() );
|
||||||
@ -99,8 +81,7 @@ public class EncodedWritableHandle extends HandleGeneric
|
|||||||
throw new LuaException( e.getMessage() );
|
throw new LuaException( e.getMessage() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case 2:
|
case 2: // flush
|
||||||
// flush
|
|
||||||
checkOpen();
|
checkOpen();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -111,8 +92,7 @@ public class EncodedWritableHandle extends HandleGeneric
|
|||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
case 3:
|
case 3: // close
|
||||||
// close
|
|
||||||
close();
|
close();
|
||||||
return null;
|
return null;
|
||||||
default:
|
default:
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
package dan200.computercraft.core.apis.http;
|
package dan200.computercraft.core.apis.http;
|
||||||
|
|
||||||
import com.google.common.base.Objects;
|
import com.google.common.base.Objects;
|
||||||
|
import com.google.common.base.Strings;
|
||||||
import dan200.computercraft.api.lua.ILuaContext;
|
import dan200.computercraft.api.lua.ILuaContext;
|
||||||
import dan200.computercraft.api.lua.ILuaObject;
|
import dan200.computercraft.api.lua.ILuaObject;
|
||||||
import dan200.computercraft.api.lua.LuaException;
|
import dan200.computercraft.api.lua.LuaException;
|
||||||
@ -26,7 +27,7 @@ import io.netty.util.CharsetUtil;
|
|||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import static dan200.computercraft.core.apis.ArgumentHelper.optBoolean;
|
import static dan200.computercraft.core.apis.ArgumentHelper.optBoolean;
|
||||||
|
|
||||||
@ -68,15 +69,20 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException
|
public void close()
|
||||||
{
|
{
|
||||||
close( false );
|
close( false );
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onClosed()
|
private void onClosed( int status, String reason )
|
||||||
{
|
{
|
||||||
close( true );
|
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
|
@Override
|
||||||
@ -96,12 +102,12 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
|||||||
@Override
|
@Override
|
||||||
public void channelInactive( ChannelHandlerContext ctx ) throws Exception
|
public void channelInactive( ChannelHandlerContext ctx ) throws Exception
|
||||||
{
|
{
|
||||||
onClosed();
|
onClosed( -1, "Websocket is inactive" );
|
||||||
super.channelInactive( ctx );
|
super.channelInactive( ctx );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelRead0( ChannelHandlerContext ctx, Object msg ) throws Exception
|
public void channelRead0( ChannelHandlerContext ctx, Object msg )
|
||||||
{
|
{
|
||||||
Channel ch = ctx.channel();
|
Channel ch = ctx.channel();
|
||||||
if( !handshaker.isHandshakeComplete() )
|
if( !handshaker.isHandshakeComplete() )
|
||||||
@ -123,7 +129,7 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
|||||||
String data = ((TextWebSocketFrame) frame).text();
|
String data = ((TextWebSocketFrame) frame).text();
|
||||||
|
|
||||||
computer.addTrackingChange( TrackingField.WEBSOCKET_INCOMING, data.length() );
|
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 )
|
else if( frame instanceof BinaryWebSocketFrame )
|
||||||
{
|
{
|
||||||
@ -132,12 +138,13 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
|||||||
data.readBytes( converted );
|
data.readBytes( converted );
|
||||||
|
|
||||||
computer.addTrackingChange( TrackingField.WEBSOCKET_INCOMING, converted.length );
|
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 )
|
else if( frame instanceof CloseWebSocketFrame )
|
||||||
{
|
{
|
||||||
|
CloseWebSocketFrame closeFrame = (CloseWebSocketFrame) frame;
|
||||||
ch.close();
|
ch.close();
|
||||||
onClosed();
|
onClosed( closeFrame.statusCode(), closeFrame.reasonText() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,17 +171,17 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
|||||||
{
|
{
|
||||||
switch( method )
|
switch( method )
|
||||||
{
|
{
|
||||||
case 0:
|
case 0: // receive
|
||||||
while( true )
|
while( true )
|
||||||
{
|
{
|
||||||
checkOpen();
|
checkOpen();
|
||||||
Object[] event = context.pullEvent( MESSAGE_EVENT );
|
Object[] event = context.pullEvent( MESSAGE_EVENT );
|
||||||
if( event.length >= 3 && Objects.equal( event[1], url ) )
|
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();
|
checkOpen();
|
||||||
String text = arguments.length > 0 && arguments[0] != null ? arguments[0].toString() : "";
|
String text = arguments.length > 0 && arguments[0] != null ? arguments[0].toString() : "";
|
||||||
|
Loading…
Reference in New Issue
Block a user