Created
July 10, 2014 18:31
-
-
Save NiteshKant/fb858f225f730926db2d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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. | |
*/ | |
import io.netty.buffer.ByteBuf; | |
import io.reactivex.netty.RxNetty; | |
import io.reactivex.netty.pipeline.PipelineConfigurators; | |
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.text.sse.ServerSentEvent; | |
import rx.Observable; | |
import rx.exceptions.OnErrorThrowable; | |
import rx.functions.Func1; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* @author Nitesh Kant | |
*/ | |
public class Example { | |
public static void main(String[] args) { | |
RxNetty.createHttpServer( | |
8888, | |
new RequestHandler<ByteBuf, ServerSentEvent>() { | |
@Override | |
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, | |
final HttpServerResponse<ServerSentEvent> response) { | |
return Observable.interval(1, TimeUnit.SECONDS) | |
.map(new Func1<Long, String>() { | |
@Override | |
public String call(Long aLong) { | |
return String.valueOf(aLong); | |
} | |
}) | |
.flatMap(new Func1<String, Observable<Void>>() { | |
@Override | |
public Observable<Void> call(String value) { | |
response.getHeaders().set("Access-Control-Allow-Origin", "*"); | |
response.getHeaders().set("content-type", "text/event-stream"); | |
return response.writeAndFlush(new ServerSentEvent("1", "data", value)); | |
} | |
}) | |
.onErrorFlatMap(new Func1<OnErrorThrowable, Observable<Void>>() { | |
@Override | |
public Observable<Void> call(OnErrorThrowable error) { | |
error.printStackTrace(); | |
return response.writeStringAndFlush(error.getMessage()); | |
} | |
}) | |
.onErrorFlatMap(new Func1<OnErrorThrowable, Observable<Void>>() { | |
// There's nothing we can do this time, so we simply report the error | |
@Override | |
public Observable<Void> call(OnErrorThrowable error) { | |
error.printStackTrace(); | |
return Observable.empty(); | |
} | |
}); | |
} | |
}, | |
PipelineConfigurators.<ByteBuf>sseServerConfigurator() | |
).startAndWait(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment