All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.reactivex.netty.examples.http.sse.HttpSseServer Maven / Gradle / Ivy

/*
 * Copyright 2014 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.reactivex.netty.examples.http.sse;

import io.netty.buffer.ByteBuf;
import io.reactivex.netty.RxNetty;
import io.reactivex.netty.pipeline.PipelineConfigurators;
import io.reactivex.netty.protocol.http.server.HttpServer;
import io.reactivex.netty.protocol.http.server.HttpServerRequest;
import io.reactivex.netty.protocol.http.server.HttpServerResponse;
import io.reactivex.netty.protocol.http.server.RequestHandler;
import io.reactivex.netty.protocol.http.sse.ServerSentEvent;
import rx.Notification;
import rx.Observable;
import rx.functions.Func1;

import java.util.concurrent.TimeUnit;

/**
 * @author Nitesh Kant
 */
public final class HttpSseServer {

    static final int DEFAULT_PORT = 8096;
    static final int DEFAULT_INTERVAL = 1000;

    private final int port;
    private final int interval;

    public HttpSseServer(int port, int interval) {
        this.port = port;
        this.interval = interval;
    }

    public HttpServer createServer() {
        HttpServer server = RxNetty.createHttpServer(port,
                new RequestHandler() {
                    @Override
                    public Observable handle(HttpServerRequest request,
                                                   HttpServerResponse response) {
                        return getIntervalObservable(response);
                    }
                }, PipelineConfigurators.serveSseConfigurator());
        System.out.println("HTTP Server Sent Events server started...");
        return server;
    }

    private Observable getIntervalObservable(final HttpServerResponse response) {
        return Observable.interval(interval, TimeUnit.MILLISECONDS)
                .flatMap(new Func1>() {
                    @Override
                    public Observable call(Long interval) {
                        System.out.println("Writing SSE event for interval: " + interval);
                        ByteBuf data = response.getAllocator().buffer().writeBytes(("hello " + interval).getBytes());
                        ServerSentEvent event = new ServerSentEvent(data);
                        return response.writeAndFlush(event);
                    }
                }).materialize()
                .takeWhile(new Func1, Boolean>() {
                    @Override
                    public Boolean call(Notification notification) {
                        if (notification.isOnError()) {
                            System.out.println("Write to client failed, stopping response sending.");
                            notification.getThrowable().printStackTrace(System.err);
                        }
                        return !notification.isOnError();
                    }
                })
                .map(new Func1, Void>() {
                    @Override
                    public Void call(Notification notification) {
                        return null;
                    }
                });
    }

    public static void main(String[] args) {
        new HttpSseServer(DEFAULT_PORT, DEFAULT_INTERVAL).createServer().startAndWait();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy