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

Handle websockets which close while receiving

This changes the previous behaviour a little, but hopefully is more
sane:

 - Only require the socket to be open when first calling receive. This
   means if it closes while receving, you won't get an error.

   This behaviour is still not perfect - the socket could have closed,
   but the event not reached the user yet, but it's better.

 - Listen to websocket_close events while receiving, and return null
   should it match ours.

See #201
This commit is contained in:
SquidDev 2019-05-08 08:09:56 +01:00
parent 7f2471d6b2
commit 0ec3884e98

View File

@ -24,6 +24,7 @@ import java.io.Closeable;
import java.util.Arrays; import java.util.Arrays;
import static dan200.computercraft.core.apis.ArgumentHelper.optBoolean; import static dan200.computercraft.core.apis.ArgumentHelper.optBoolean;
import static dan200.computercraft.core.apis.http.websocket.Websocket.CLOSE_EVENT;
import static dan200.computercraft.core.apis.http.websocket.Websocket.MESSAGE_EVENT; import static dan200.computercraft.core.apis.http.websocket.Websocket.MESSAGE_EVENT;
public class WebsocketHandle implements ILuaObject, Closeable public class WebsocketHandle implements ILuaObject, Closeable
@ -53,15 +54,18 @@ public class WebsocketHandle implements ILuaObject, Closeable
switch( method ) switch( method )
{ {
case 0: // receive case 0: // receive
checkOpen();
while( true ) while( true )
{ {
checkOpen(); Object[] event = context.pullEvent( null );
if( event.length >= 3 && Objects.equal( event[0], MESSAGE_EVENT ) && Objects.equal( event[1], websocket.address() ) )
Object[] event = context.pullEvent( MESSAGE_EVENT );
if( event.length >= 3 && Objects.equal( event[1], websocket.address() ) )
{ {
return Arrays.copyOfRange( event, 2, event.length ); return Arrays.copyOfRange( event, 2, event.length );
} }
else if( event.length >= 2 && Objects.equal( event[0], CLOSE_EVENT ) && Objects.equal( event[1], websocket.address() ) && closed )
{
return null;
}
} }
case 1: // send case 1: // send