-
Notifications
You must be signed in to change notification settings - Fork 357
provides draft of the enhanced websocket handling #691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
41777f1
provides draft of the enhanced websocket handling
OlegDokuka c7e01f4
fixes samples; bumps reactor to RC1 version; bumps netty to 4.1.39
OlegDokuka c51ae4d
fixes samples
OlegDokuka a1f4d8e
fixes build script
OlegDokuka b42f27b
Update rsocket-transport-netty/src/main/java/io/rsocket/transport/net…
OlegDokuka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
138 changes: 138 additions & 0 deletions
138
.../src/main/java/io/rsocket/examples/transport/ws/WebSocketRouteTransportHeadersSample.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* | ||
| * Copyright 2015-2018 the original author or authors. | ||
| * | ||
| * 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.rsocket.examples.transport.ws; | ||
|
|
||
| import io.netty.handler.codec.http.websocketx.WebSocketCloseStatus; | ||
| import io.rsocket.AbstractRSocket; | ||
| import io.rsocket.ConnectionSetupPayload; | ||
| import io.rsocket.Payload; | ||
| import io.rsocket.RSocket; | ||
| import io.rsocket.RSocketFactory; | ||
| import io.rsocket.SocketAcceptor; | ||
| import io.rsocket.frame.decoder.PayloadDecoder; | ||
| import io.rsocket.transport.netty.client.WebsocketClientTransport; | ||
| import io.rsocket.transport.netty.server.CloseableChannel; | ||
| import io.rsocket.transport.netty.server.WebsocketRouteTransport; | ||
| import io.rsocket.util.ByteBufPayload; | ||
| import java.time.Duration; | ||
| import java.util.HashMap; | ||
| import org.reactivestreams.Publisher; | ||
| import reactor.core.publisher.Flux; | ||
| import reactor.core.publisher.Mono; | ||
| import reactor.core.publisher.MonoProcessor; | ||
| import reactor.core.scheduler.Schedulers; | ||
| import reactor.netty.http.server.HttpServer; | ||
|
|
||
| public class WebSocketRouteTransportHeadersSample { | ||
| static final Payload payload1 = ByteBufPayload.create("Hello "); | ||
|
|
||
| public static void main(String[] args) { | ||
|
|
||
| CloseableChannel disposableServer = | ||
| RSocketFactory.receive() | ||
| .frameDecoder(PayloadDecoder.ZERO_COPY) | ||
| .acceptor(new SocketAcceptorImpl()) | ||
| .transport( | ||
| // Same could be done with routing transport | ||
| WebsocketRouteTransport.builder() | ||
| .filteringInbound( | ||
| headers -> headers.containsValue("Authorization", "test", true)) | ||
| .closingWithStatus(headers -> new WebSocketCloseStatus(4404, "Unauthorized")) | ||
| .observingOn("/") | ||
| .build(HttpServer.create().host("localhost").port(8080))) | ||
| .start() | ||
| .block(); | ||
|
|
||
| WebsocketClientTransport clientTransport = | ||
| WebsocketClientTransport.create(disposableServer.address()); | ||
|
|
||
| MonoProcessor<WebSocketCloseStatus> statusMonoProcessor = MonoProcessor.create(); | ||
| clientTransport.setTransportHeaders( | ||
| () -> { | ||
| HashMap<String, String> map = new HashMap<>(); | ||
| map.put("Authorization", "1"); | ||
| return map; | ||
| }); | ||
|
|
||
| clientTransport.setCloseStatusConsumer( | ||
| webSocketCloseStatusMono -> webSocketCloseStatusMono.log().subscribe(statusMonoProcessor)); | ||
|
|
||
| RSocket socket = | ||
| RSocketFactory.connect() | ||
| .keepAliveAckTimeout(Duration.ofMinutes(10)) | ||
| .frameDecoder(PayloadDecoder.ZERO_COPY) | ||
| .transport(clientTransport) | ||
| .start() | ||
| .block(); | ||
|
|
||
| try { | ||
| Flux.range(0, 100).concatMap(i -> socket.fireAndForget(payload1.retain())).blockLast(); | ||
|
|
||
| } catch (Exception e) { | ||
| System.out.println("Observed WebSocket Close Status " + statusMonoProcessor.peek()); | ||
| } | ||
|
|
||
| socket.dispose(); | ||
|
|
||
| WebsocketClientTransport clientTransport2 = | ||
| WebsocketClientTransport.create(disposableServer.address()); | ||
|
|
||
| clientTransport2.setTransportHeaders( | ||
| () -> { | ||
| HashMap<String, String> map = new HashMap<>(); | ||
| map.put("Authorization", "test"); | ||
| return map; | ||
| }); | ||
|
|
||
| RSocket rSocket = | ||
| RSocketFactory.connect() | ||
| .keepAliveAckTimeout(Duration.ofMinutes(10)) | ||
| .frameDecoder(PayloadDecoder.ZERO_COPY) | ||
| .transport(clientTransport2) | ||
| .start() | ||
| .block(); | ||
|
|
||
| // expect normal execution here | ||
| rSocket.requestResponse(payload1).block(); | ||
| } | ||
|
|
||
| private static class SocketAcceptorImpl implements SocketAcceptor { | ||
| @Override | ||
| public Mono<RSocket> accept(ConnectionSetupPayload setupPayload, RSocket reactiveSocket) { | ||
| return Mono.just( | ||
| new AbstractRSocket() { | ||
|
|
||
| @Override | ||
| public Mono<Void> fireAndForget(Payload payload) { | ||
| // System.out.println(payload.getDataUtf8()); | ||
| payload.release(); | ||
| return Mono.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<Payload> requestResponse(Payload payload) { | ||
| return Mono.just(payload); | ||
| } | ||
|
|
||
| @Override | ||
| public Flux<Payload> requestChannel(Publisher<Payload> payloads) { | ||
| return Flux.from(payloads).subscribeOn(Schedulers.single()); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.