本文整理汇总了Java中org.springframework.web.reactive.socket.WebSocketMessage类的典型用法代码示例。如果您正苦于以下问题:Java WebSocketMessage类的具体用法?Java WebSocketMessage怎么用?Java WebSocketMessage使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WebSocketMessage类属于org.springframework.web.reactive.socket包,在下文中一共展示了WebSocketMessage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handle
import org.springframework.web.reactive.socket.WebSocketMessage; //导入依赖的package包/类
@Override
public Mono<Void> handle(WebSocketSession session) {
return session
.receive()
.log("inbound-incoming-chat-message")
.map(WebSocketMessage::getPayloadAsText)
.log("inbound-convert-to-text")
.map(s -> session.getId() + ": " + s)
.log("inbound-mark-with-session-id")
.flatMap(this::broadcast)
.log("inbound-broadcast-to-broker")
.then();
}
开发者ID:PacktPublishing,项目名称:Learning-Spring-Boot-2.0-Second-Edition,代码行数:14,代码来源:InboundChatService.java
示例2: main
import org.springframework.web.reactive.socket.WebSocketMessage; //导入依赖的package包/类
public static void main(String[] args) throws URISyntaxException {
WebSocketClient client = new ReactorNettyWebSocketClient();
client.execute(URI.create(BITFINEX),
session -> session.send(Mono.just(session.textMessage("{\"event\": \"subscribe\", \"channel\": \"ticker\", \"pair\": \"BTCUSD\" }")))
// .and(session.send(Mono.just(session.textMessage("{\"event\": \"subscribe\", \"channel\": \"ticker\", \"pair\": \"ETHUSD\" }"))))
// .and(session.send(Mono.just(session.textMessage("{\"event\": \"subscribe\", \"channel\": \"ticker\", \"pair\": \"LTCUSD\" }"))))
// .and(session.send(Mono.just(session.textMessage("{\"event\": \"subscribe\", \"channel\": \"ticker\", \"pair\": \"XRPUSD\" }"))))
.thenMany(session
.receive()
.map(WebSocketMessage::getPayloadAsText)
.filter(msg -> msg.indexOf("\"hb\"") < 0)
.map(msg -> {
String[] strs = msg.replace(']', ' ').trim().split(",");
if(strs.length > 9) {
// System.out.println(strs[1]+" "+strs[2]+" "+strs[3]+" "+strs[4]+" "+strs[5]+" "+strs[6]+" "+strs[7]+" "+strs[8]+" "+strs[9]+" "+strs[10]);
BigDecimal mid = (new BigDecimal(strs[9]).add(new BigDecimal(strs[10].trim()))).divide(BigDecimal.valueOf(2));
QuoteBf qbf = new QuoteBf(mid, new BigDecimal(strs[1]), new BigDecimal(strs[3]), new BigDecimal(strs[7]), new BigDecimal(strs[10]), new BigDecimal(strs[9]), new BigDecimal(strs[8]), "");
qbf.setPair("BTCUSD");
return Optional.of(qbf);
}
return Optional.empty();
})
.log())
.then()).block();
System.out.println("End");
}
示例3: handlerMapping
import org.springframework.web.reactive.socket.WebSocketMessage; //导入依赖的package包/类
@Bean
public HandlerMapping handlerMapping() {
Map<String, WebSocketHandler> mappings = new HashMap<>(2);
mappings.put("/in", session -> {
session.receive()
.map(WebSocketMessage::getPayloadAsText)
.log()
.subscribe(out::onNext);
return session.send(Mono.just(session.textMessage(MESSAGE_HELLO))).then(session.send(in.map(session::textMessage)));
});
mappings.put("/out", session -> {
session.receive()
.map(WebSocketMessage::getPayloadAsText)
.log()
.subscribe();
return session.send(Mono.just(session.textMessage(MESSAGE_HELLO))).then(session.send(out.map(session::textMessage)));
});
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setUrlMap(mappings);
mapping.setOrder(10);
return mapping;
}
示例4: handle
import org.springframework.web.reactive.socket.WebSocketMessage; //导入依赖的package包/类
@Override
public Mono<Void> handle(WebSocketSession session) {
// pass headers along so custom headers can be sent through
return client.execute(url, this.headers, new WebSocketHandler() {
@Override
public Mono<Void> handle(WebSocketSession proxySession) {
// Use retain() for Reactor Netty
Mono<Void> proxySessionSend = proxySession
.send(session.receive().doOnNext(WebSocketMessage::retain));
// .log("proxySessionSend", Level.FINE);
Mono<Void> serverSessionSend = session
.send(proxySession.receive().doOnNext(WebSocketMessage::retain));
// .log("sessionSend", Level.FINE);
return Mono.when(proxySessionSend, serverSessionSend).then();
}
/**
* Copy subProtocols so they are available downstream.
* @return
*/
@Override
public List<String> getSubProtocols() {
return ProxyWebSocketHandler.this.subProtocols;
}
});
}
示例5: echo
import org.springframework.web.reactive.socket.WebSocketMessage; //导入依赖的package包/类
@Test
public void echo() throws Exception {
int count = 100;
Flux<String> input = Flux.range(1, count).map(index -> "msg-" + index);
ReplayProcessor<Object> output = ReplayProcessor.create(count);
client.execute(getUrl("/echo"),
session -> {
logger.debug("Starting to send messages");
return session
.send(input.doOnNext(s -> logger.debug("outbound " + s)).map(session::textMessage))
.thenMany(session.receive().take(count).map(WebSocketMessage::getPayloadAsText))
.subscribeWith(output)
.doOnNext(s -> logger.debug("inbound " + s))
.then()
.doOnSuccessOrError((aVoid, ex) ->
logger.debug("Done with " + (ex != null ? ex.getMessage() : "success")));
})
.block(Duration.ofMillis(5000));
assertEquals(input.collectList().block(Duration.ofMillis(5000)),
output.collectList().block(Duration.ofMillis(5000)));
}
示例6: doHandle
import org.springframework.web.reactive.socket.WebSocketMessage; //导入依赖的package包/类
@Override
protected Mono<Void> doHandle(WebSocketSession session) {
//end::code[]
return session.receive()
.log(session.getId()
+ "-inbound-incoming-chat-message")
.map(WebSocketMessage::getPayloadAsText)
.log(session.getId()
+ "-inbound-convert-to-text")
.flatMap(message -> broadcast(message, session))
.log(session.getId()
+ "-inbound-broadcast-to-broker")
.then();
}
开发者ID:PacktPublishing,项目名称:Learning-Spring-Boot-2.0-Second-Edition,代码行数:15,代码来源:InboundChatService.java
示例7: handleInternal
import org.springframework.web.reactive.socket.WebSocketMessage; //导入依赖的package包/类
@Override
protected Mono<Void> handleInternal(WebSocketSession session) {
return session
.receive()
.log(getUser(session.getId())
+ "-inbound-incoming-chat-message")
.map(WebSocketMessage::getPayloadAsText)
.log(getUser(session.getId())
+ "-inbound-convert-to-text")
.flatMap(message ->
broadcast(message, getUser(session.getId())))
.log(getUser(session.getId())
+ "-inbound-broadcast-to-broker")
.then();
}
开发者ID:PacktPublishing,项目名称:Learning-Spring-Boot-2.0-Second-Edition,代码行数:16,代码来源:InboundChatService.java
示例8: main
import org.springframework.web.reactive.socket.WebSocketMessage; //导入依赖的package包/类
public static final void main(String[] args) throws URISyntaxException {
WebSocketClient client = new ReactorNettyWebSocketClient();
// client.execute(new URI("ws://localhost:8080/echo"), (WebSocketSession session) -> {
// session.send().log().;
// });
int count = 100;
Flux<String> input = Flux.range(1, count).map(index -> "msg-" + index);
ReplayProcessor<Object> output = ReplayProcessor.create(count);
client.execute(new URI("ws://localhost:8080/echo"),
session -> {
log.debug("Starting to send messages");
return session
.send(input.doOnNext(s -> log.debug("outbound " + s)).map(session::textMessage))
.thenMany(session.receive().take(count).map(WebSocketMessage::getPayloadAsText))
.subscribeWith(output)
.doOnNext(s -> log.debug("inbound " + s))
.then()
.doOnTerminate((aVoid, ex) ->
log.debug("Done with " + (ex != null ? ex.getMessage() : "success")));
})
.block(Duration.ofMillis(5000));
// assertEquals(input.collectList().block(Duration.ofMillis(5000)),
// output.collectList().block(Duration.ofMillis(5000)));
// client.execute(new URI("ws://localhost:8080/echo")), session -> {
// session.
// }
// ).blockMillis(5000);
}
示例9: main
import org.springframework.web.reactive.socket.WebSocketMessage; //导入依赖的package包/类
public static void main(String[] args) throws InterruptedException {
WebSocketClient client = new ReactorNettyWebSocketClient();
client.execute(URI.create("wss://echo.websocket.org"),
session -> session.send(Mono.just(
session.textMessage("{\"event\":\"ping\"}")))
.thenMany(session
.receive()
.map(WebSocketMessage::getPayloadAsText)
.log())
.then()).block(Duration.ofSeconds(10));
System.out.println("End");
}
示例10: subProtocol
import org.springframework.web.reactive.socket.WebSocketMessage; //导入依赖的package包/类
@Test
public void subProtocol() throws Exception {
String protocol = "echo-v1";
AtomicReference<HandshakeInfo> infoRef = new AtomicReference<>();
MonoProcessor<Object> output = MonoProcessor.create();
client.execute(getUrl("/sub-protocol"),
new WebSocketHandler() {
@Override
public List<String> getSubProtocols() {
return Collections.singletonList(protocol);
}
@Override
public Mono<Void> handle(WebSocketSession session) {
infoRef.set(session.getHandshakeInfo());
return session.receive()
.map(WebSocketMessage::getPayloadAsText)
.subscribeWith(output)
.then();
}
})
.block(Duration.ofMillis(5000));
HandshakeInfo info = infoRef.get();
assertThat(info.getHeaders().getFirst("Upgrade"), Matchers.equalToIgnoringCase("websocket"));
assertEquals(protocol, info.getHeaders().getFirst("Sec-WebSocket-Protocol"));
assertEquals("Wrong protocol accepted", protocol, info.getSubProtocol());
assertEquals("Wrong protocol detected on the server side", protocol, output.block(Duration.ofMillis(5000)));
}
示例11: customHeader
import org.springframework.web.reactive.socket.WebSocketMessage; //导入依赖的package包/类
@Test
public void customHeader() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.add("my-header", "my-value");
MonoProcessor<Object> output = MonoProcessor.create();
client.execute(getUrl("/custom-header"), headers,
session -> session.receive()
.map(WebSocketMessage::getPayloadAsText)
.subscribeWith(output)
.then())
.block(Duration.ofMillis(5000));
assertEquals("my-header:my-value", output.block(Duration.ofMillis(5000)));
}
示例12: handle
import org.springframework.web.reactive.socket.WebSocketMessage; //导入依赖的package包/类
@Override
public Mono<Void> handle(WebSocketSession session) {
String protocol = session.getHandshakeInfo().getSubProtocol();
if (!StringUtils.hasText(protocol)) {
return Mono.error(new IllegalStateException("Missing protocol"));
}
WebSocketMessage message = session.textMessage(protocol);
return doSend(session, Mono.just(message));
}
示例13: handle
import org.springframework.web.reactive.socket.WebSocketMessage; //导入依赖的package包/类
@Override
public Mono<Void> handle(WebSocketSession session) {
// Use retain() for Reactor Netty
return session.send(session.receive().doOnNext(WebSocketMessage::retain));
}
示例14: handle
import org.springframework.web.reactive.socket.WebSocketMessage; //导入依赖的package包/类
@Override
public Mono<Void> handle(WebSocketSession session) {
String protocol = session.getHandshakeInfo().getSubProtocol();
WebSocketMessage message = session.textMessage(this.posts.findAll().takeLast(0).toString());
return doSend(session, Mono.just(message));
}
示例15: doSend
import org.springframework.web.reactive.socket.WebSocketMessage; //导入依赖的package包/类
private Mono<Void> doSend(WebSocketSession session, Publisher<WebSocketMessage> output) {
return session.send(Mono.delay(Duration.ofMillis(100)).thenMany(output));
}