本文整理汇总了Java中io.vertx.core.http.WebSocket类的典型用法代码示例。如果您正苦于以下问题:Java WebSocket类的具体用法?Java WebSocket怎么用?Java WebSocket使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WebSocket类属于io.vertx.core.http包,在下文中一共展示了WebSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testConnection
import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Test
public void testConnection() {
AtomicReference<Throwable> error = new AtomicReference<>();
AtomicReference<Buffer> frame = new AtomicReference<>();
AtomicReference<WebSocket> socket = new AtomicReference<>();
vertx.createHttpClient().websocket(8080, "localhost", "/stomp", MultiMap.caseInsensitiveMultiMap().add
("Sec-WebSocket-Protocol", "v10.stomp, v11.stomp, v12.stomp"), ws -> {
socket.set(ws);
ws.exceptionHandler(error::set)
.handler(frame::set)
.write(new Frame(Frame.Command.CONNECT, Headers.create("accept-version", "1.2,1.1,1.0",
"heart-beat", "10000,10000"), null).toBuffer());
});
await().atMost(10, TimeUnit.SECONDS).until(() -> error.get() == null && frame.get() != null);
assertThat(frame.get().toString()).startsWith("CONNECTED")
.contains("server:vertx-stomp", "heart-beat:", "session:", "version:1.2");
socket.get().close();
}
示例2: testTextFrameRawWebSocket
import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Test
public void testTextFrameRawWebSocket() throws InterruptedException {
String serverPath = "/textecho";
setupSockJsServer(serverPath, this::echoRequest);
String message = "hello";
AtomicReference<String> receivedReply = new AtomicReference<>();
WebSocket ws = setupRawWebsocketClient(serverPath);
ws.handler(replyBuffer -> receivedReply.set(replyBuffer.toString()));
ws.writeFrame(WebSocketFrame.textFrame(message, true));
await(5, TimeUnit.SECONDS);
assertEquals("Client reply should have matched request", message, receivedReply.get());
}
示例3: testTextFrameSockJs
import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Test
public void testTextFrameSockJs() throws InterruptedException {
String serverPath = "/text-sockjs";
setupSockJsServer(serverPath, this::echoRequest);
List<Buffer> receivedMessages = new ArrayList<>();
WebSocket openedWebSocket = setupSockJsClient(serverPath, receivedMessages);
String messageToSend = "[\"testMessage\"]";
openedWebSocket.writeFrame(WebSocketFrame.textFrame(messageToSend, true));
await(5, TimeUnit.SECONDS);
assertEquals("Client should have received 2 messages: the reply and the close.", 2, receivedMessages.size());
Buffer expectedReply = Buffer.buffer("a" + messageToSend);
assertEquals("Client reply should have matched request", expectedReply, receivedMessages.get(0));
assertEquals("Final message should have been a close", SOCKJS_CLOSE_REPLY, receivedMessages.get(1));
}
示例4: setupSockJsClient
import io.vertx.core.http.WebSocket; //导入依赖的package包/类
/**
* This sets up a handler on the websocket
*/
private WebSocket setupSockJsClient(String serverPath, List<Buffer> receivedMessagesCollector)
throws InterruptedException
{
String requestURI = serverPath + "/000/000/websocket";
AtomicReference<WebSocket> openedWebSocketReference = new AtomicReference<>();
CountDownLatch openSocketCountDown = new CountDownLatch(1);
client.websocket(requestURI, ws -> {
openedWebSocketReference.set(ws);
ws.handler(replyBuffer -> {
log.debug("Client received " + replyBuffer);
String textReply = replyBuffer.toString();
if ("o".equals(textReply)) {
openSocketCountDown.countDown();
} else {
receivedMessagesCollector.add(replyBuffer);
}
});
ws.endHandler(v -> testComplete());
ws.exceptionHandler(this::fail);
});
openSocketCountDown.await(5, TimeUnit.SECONDS);
return openedWebSocketReference.get();
}
示例5: setupRawWebsocketClient
import io.vertx.core.http.WebSocket; //导入依赖的package包/类
/**
* This does not set up a handler on the websocket
*/
private WebSocket setupRawWebsocketClient(String serverPath)
throws InterruptedException
{
String requestURI = serverPath + "/websocket";
AtomicReference<WebSocket> openedWebSocketReference = new AtomicReference<>();
CountDownLatch openSocketCountDown = new CountDownLatch(1);
client.websocket(requestURI, ws -> {
openedWebSocketReference.set(ws);
openSocketCountDown.countDown();
ws.endHandler(v -> testComplete());
ws.exceptionHandler(this::fail);
});
openSocketCountDown.await(5, TimeUnit.SECONDS);
return openedWebSocketReference.get();
}
示例6: sendHeartbeat
import io.vertx.core.http.WebSocket; //导入依赖的package包/类
private void sendHeartbeat(WebSocket ws) {
try {
ws.writeFrame(new WebSocketFrameImpl(FrameType.PING));
} catch (IllegalStateException e) {
LOGGER.error("heartbeat fail", e);
}
}
示例7: connected
import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Override
public Map<HttpClientMetrics, ?> connected(Map<HttpClientMetrics, ?> endpointMetric,
Map<HttpClientMetrics, ?> socketMetric,
WebSocket webSocket) {
return unmap2WithResult(endpointMetric, socketMetric,
(m, ec, sc) -> m.connected(ec, sc, webSocket));
}
示例8: connected
import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Override
public StopWatch connected(StopWatch endpointWatch, StopWatch socketWatch, WebSocket webSocket) {
counterService.increment("websockets.connected");
StopWatch websocketWatch = new StopWatch();
websocketWatch.start("websocket");
return websocketWatch;
}
示例9: connected
import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Override
public SocketAddress connected(Void aVoid, SocketAddress socketAddress, WebSocket webSocket) {
long value = wsConnections.incrementAndGet();
setMetric(SENSISION_CLASS_WEBSOCKETS, defaultLabels, value);
incrementMetric(SENSISION_CLASS_WEBSOCKET_CONNECTED_COUNT, defaultLabels);
return null;
}
示例10: testSendingAMessage
import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Test
public void testSendingAMessage() {
AtomicReference<Throwable> error = new AtomicReference<>();
AtomicReference<Frame> frame = new AtomicReference<>();
AtomicReference<WebSocket> socket = new AtomicReference<>();
AtomicReference<StompClientConnection> client = new AtomicReference<>();
clients.add(StompClient.create(vertx).connect(61613, "localhost", connection -> {
connection.result().subscribe("foo", frame::set, r -> {
client.set(connection.result());
});
}));
await().atMost(10, TimeUnit.SECONDS).until(() -> client.get() != null);
await().atMost(10, TimeUnit.SECONDS).until(() -> server.stompHandler().getDestination("foo") != null);
vertx.createHttpClient().websocket(8080, "localhost", "/stomp", MultiMap.caseInsensitiveMultiMap().add
("Sec-WebSocket-Protocol", "v10.stomp, v11.stomp, v12.stomp"), ws -> {
socket.set(ws);
ws.exceptionHandler(error::set)
.handler(buffer -> {
if (buffer.toString().startsWith("CONNECTED")) {
ws.write(
new Frame(Frame.Command.SEND, Headers.create("header", "value", "destination", "foo"), Buffer
.buffer("hello")).toBuffer());
}
})
.write(new Frame(Frame.Command.CONNECT, Headers.create("accept-version", "1.2,1.1,1.0",
"heart-beat", "10000,10000"), null).toBuffer());
});
await().atMost(10, TimeUnit.SECONDS).until(() -> error.get() == null && frame.get() != null);
assertThat(frame.get().toString()).startsWith("MESSAGE")
.contains("destination:foo", "header:value", "\nhello");
socket.get().close();
}
示例11: testPingFromServer
import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Test
public void testPingFromServer() {
AtomicReference<Throwable> error = new AtomicReference<>();
AtomicReference<WebSocket> socket = new AtomicReference<>();
AtomicReference<Boolean> flag = new AtomicReference<>();
AtomicReference<StompClientConnection> client = new AtomicReference<>();
clients.add(StompClient.create(vertx).connect(61613, "localhost", connection -> {
client.set(connection.result());
}));
await().atMost(10, TimeUnit.SECONDS).until(() -> client.get() != null);
vertx.createHttpClient().websocket(8080, "localhost", "/stomp", MultiMap.caseInsensitiveMultiMap().add
("Sec-WebSocket-Protocol", "v10.stomp, v11.stomp, v12.stomp"), ws -> {
socket.set(ws);
ws.exceptionHandler(error::set)
.handler(buffer -> {
vertx.setTimer(1000, id -> {
flag.set(true);
});
})
.write(new Frame(Frame.Command.CONNECT, Headers.create("accept-version", "1.2,1.1,1.0",
"heart-beat", "100,0"), null).toBuffer());
});
await().atMost(10, TimeUnit.SECONDS).until(() -> error.get() == null && flag.get() != null);
socket.get().close();
}
示例12: sendTypeToBridge
import io.vertx.core.http.WebSocket; //导入依赖的package包/类
protected static void sendTypeToBridge(WebSocket ws, String type, String address, String msg) {
JsonObject json = new JsonObject();
json.put("type", type);
json.put("address", address);
json.put("body", msg);
ws.write(Buffer.buffer(json.toString()));
}
示例13: connected
import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Override
public SocketAddress connected(Void endpointMetric, SocketAddress key, WebSocket webSocket) {
HttpClientConnectionsMeasurements measurements = connectionsMeasurements.get(key);
if (measurements != null) {
measurements.incrementWsConnectionCount();
}
return key;
}
示例14: testCombineBinaryContinuationFramesRawWebSocket
import io.vertx.core.http.WebSocket; //导入依赖的package包/类
/**
* Writing multiple continuation frames from the client side should result in a single message on the server side
* after the frames are re-combined
*/
@Test
public void testCombineBinaryContinuationFramesRawWebSocket() throws InterruptedException {
String serverPath = "/combine";
AtomicReference<Buffer> serverReceivedMessage = new AtomicReference<>();
setupSockJsServer(serverPath, (sock, requestBuffer) -> {
serverReceivedMessage.set(requestBuffer);
sock.write(Buffer.buffer("reply"));
sock.close();
});
Buffer largeMessage = Buffer.buffer(TestUtils.randomAlphaString(30));
WebSocketFrame frame1 = WebSocketFrame.binaryFrame(largeMessage.slice(0, 10), false);
WebSocketFrame frame2 = WebSocketFrame.continuationFrame(largeMessage.slice(10, 20), false);
WebSocketFrame frame3 = WebSocketFrame.continuationFrame(largeMessage.slice(20, largeMessage.length()), true);
WebSocket ws = setupRawWebsocketClient(serverPath);
ws.writeFrame(frame1);
ws.writeFrame(frame2);
ws.writeFrame(frame3);
await(5, TimeUnit.SECONDS);
assertEquals("Server did not combine continuation frames correctly", largeMessage, serverReceivedMessage.get());
}
示例15: testSplitLargeReplyRawWebSocket
import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Test
public void testSplitLargeReplyRawWebSocket() throws InterruptedException {
String serverPath = "/split";
String largeReply = TestUtils.randomAlphaString(65536 * 5);
Buffer largeReplyBuffer = Buffer.buffer(largeReply);
setupSockJsServer(serverPath, (sock, requestBuffer) -> {
sock.write(largeReplyBuffer);
sock.close();
});
Buffer totalReplyBuffer = Buffer.buffer(largeReplyBuffer.length());
AtomicInteger receivedReplies = new AtomicInteger(0);
WebSocket ws = setupRawWebsocketClient(serverPath);
ws.handler(replyBuffer -> {
totalReplyBuffer.appendBuffer(replyBuffer);
receivedReplies.incrementAndGet();
});
ws.writeFrame(WebSocketFrame.binaryFrame(Buffer.buffer("hello"), true));
await(5, TimeUnit.SECONDS);
int receivedReplyCount = receivedReplies.get();
assertEquals("Combined reply on client should equal message from server", largeReplyBuffer, totalReplyBuffer);
assertTrue("Should have received > 1 reply frame, actually received " + receivedReplyCount, receivedReplyCount > 1);
}