Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

server sent events httpclient java.net

public class SseSubscriber implements BodySubscriber<Void>
{
    protected static final Pattern dataLinePattern = Pattern.compile( "^data: ?(.*)$" );

    protected static String extractMessageData( String[] messageLines )
    {
        var s = new StringBuilder( );
        for ( var line : messageLines )
        {
            var m = dataLinePattern.matcher( line );
            if ( m.matches( ) )
            {
                s.append( m.group( 1 ) );
            }
        }
        return s.toString( );
    }

    protected final Consumer<? super String> messageDataConsumer;
    protected final CompletableFuture<Void> future;
    protected volatile Subscription subscription;
    protected volatile String deferredText;

    public SseSubscriber( Consumer<? super String> messageDataConsumer )
    {
        this.messageDataConsumer = messageDataConsumer;
        this.future = new CompletableFuture<>( );
        this.subscription = null;
        this.deferredText = null;
    }

    @Override
    public void onSubscribe( Subscription subscription )
    {
        this.subscription = subscription;
        try
        {
            this.deferredText = "";
            this.subscription.request( 1 );
        }
        catch ( Exception e )
        {
            this.future.completeExceptionally( e );
            this.subscription.cancel( );
        }
    }

    @Override
    public void onNext( List<ByteBuffer> buffers )
    {
        try
        {
            // Volatile read
            var deferredText = this.deferredText;

            for ( var buffer : buffers )
            {
                // TODO: Safe to assume multi-byte chars don't get split across buffers?
                var s = deferredText + UTF_8.decode( buffer );

                // -1 means don't discard trailing empty tokens ... so the final token will
                // be whatever is left after the last 

 (possibly the empty string, but
                // not necessarily), which is the part we need to defer until the next loop
                // iteration
                var tokens = s.split( "

", -1 );

                // Final token gets deferred, not processed here
                for ( var i = 0; i < tokens.length - 1; i++ )
                {
                    var message = tokens[ i ];
                    var lines = message.split( "
" );
                    var data = extractMessageData( lines );
                    this.messageDataConsumer.accept( data );
                    // TODO: Handle lines that start with "event:", "id:", "retry:"
                }

                // Defer the final token
                deferredText = tokens[ tokens.length - 1 ];
            }

            // Volatile write
            this.deferredText = deferredText;

            this.subscription.request( 1 );
        }
        catch ( Exception e )
        {
            this.future.completeExceptionally( e );
            this.subscription.cancel( );
        }
    }

    @Override
    public void onError( Throwable e )
    {
        this.future.completeExceptionally( e );
    }

    @Override
    public void onComplete( )
    {
        try
        {
            this.future.complete( null );
        }
        catch ( Exception e )
        {
            this.future.completeExceptionally( e );
        }
    }

    @Override
    public CompletionStage<Void> getBody( )
    {
        return this.future;
    }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: get_refreshed_fragments too long to load 
Typescript :: how to collect array of objects in one value key in laravel 
Typescript :: how to change woocommerce header message This is where you can add new products to your store. 
Typescript :: requests session next page python 
Typescript :: components of .net framework 
Typescript :: typescript directory structure 
Typescript :: typescript dictionary usestate 
Typescript :: minimum number of cycle shifts for each string if it can be made palindrome 
Typescript :: how should a developer write unit tests for a private method in an apex class 
Typescript :: 3 dots for edit bootstrap 
Typescript :: which of the foolowing ia an element of pallette that holds multiple elements of nspecific purpose 
Typescript :: running same tests against different data 
Typescript :: combine 2 lists to dataframe python 
Typescript :: typescript where to put interfaces 
Typescript :: t sql if exists multiple conditions 
Typescript :: 3d plot goes across limits python 
Typescript :: google fonts roboto 
Cpp :: conda list envs 
Cpp :: a c++ program to set a countdown timer 
Cpp :: 2d vector print 
Cpp :: how to check string contains char in c++ 
Cpp :: on component end overlap c++ 
Cpp :: c++ try catch 
Cpp :: c++ get cursor position console 
Cpp :: multiply two Mat in c++ element per element 
Cpp :: how to sort a 2d array in c++ 
Cpp :: eosio get time 
Cpp :: c++ wait for user input 
Cpp :: c++ uniform_real_distribution get same result 
Cpp :: size of 2d array in c++ 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =