本文整理汇总了Java中io.undertow.websockets.core.WebSocketChannel类的典型用法代码示例。如果您正苦于以下问题:Java WebSocketChannel类的具体用法?Java WebSocketChannel怎么用?Java WebSocketChannel使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WebSocketChannel类属于io.undertow.websockets.core包,在下文中一共展示了WebSocketChannel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onConnect
import io.undertow.websockets.core.WebSocketChannel; //导入依赖的package包/类
@Override
public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
String uri = exchange.getRequestURI();
final String subscriptionId = uri.substring(path.length() + 1);
EventReservationsHandler.Reservation reservation = eventReservationsHandler.claimReservation(subscriptionId);
if (reservation == null) {
send(channel, "error", "Invalid subscription: not reserved");
safeClose(channel);
return;
}
LOG.debug("Principal is: {}", reservation.getPrincipal());
send(channel, "message", "connected");
bus.subscribe(subscriptionId, (type, data) -> {
if (channel.isOpen()) {
send(channel, type, data);
} else {
bus.unsubscribe(subscriptionId);
}
});
}
示例2: accept
import io.undertow.websockets.core.WebSocketChannel; //导入依赖的package包/类
public void accept(WebSocketChannel channel, User user, Object obj, String ip) {
if (user != null) {
if (obj instanceof ClientPlace) {
handlePlace(channel, user, ((ClientPlace) obj), ip);
}
if (obj instanceof ClientUndo) handleUndo(channel, user, ((ClientUndo) obj));
if (obj instanceof ClientCaptcha) handleCaptcha(channel, user, ((ClientCaptcha) obj));
if (obj instanceof ClientShadowBanMe) handleShadowBanMe(channel, user, ((ClientShadowBanMe) obj));
if (obj instanceof ClientBanMe) handleBanMe(channel, user, ((ClientBanMe) obj));
if (user.getRole().greaterEqual(Role.MODERATOR)) {
if (obj instanceof ClientAdminCooldownOverride)
handleCooldownOverride(channel, user, ((ClientAdminCooldownOverride) obj));
if (obj instanceof ClientAdminMessage)
handleAdminMessage(channel, user, ((ClientAdminMessage) obj));
}
}
}
示例3: connectBlocking
import io.undertow.websockets.core.WebSocketChannel; //导入依赖的package包/类
@Override
public boolean connectBlocking(long timeout) throws IOException {
logger.debug("Connect blocking... ({})", this);
IoFuture<WebSocketChannel> futureChan = connectionBuilder().connect();
IoFuture.Status status = futureChan.await(timeout, TimeUnit.MILLISECONDS);
logger.debug("Connect blocking status: {}", status);
switch (status) {
case DONE:
// ok
this.channel = futureChan.get();
registerChannelReceivers();
return true;
default:
handlerService.submit(() -> onError(futureChan.getException()));
try {
Thread.sleep(reconnectDelay);
return this.connectBlocking(timeout);
} catch (InterruptedException e) {
logger.warn("Connect blocking interrupted while sleeping", e);
}
// error or interrupted or timed-out
return false;
}
}
示例4: registerWebSocketChannelListener
import io.undertow.websockets.core.WebSocketChannel; //导入依赖的package包/类
private void registerWebSocketChannelListener(WebSocketChannel webSocketChannel) {
ChannelListener<WebSocketChannel> listener = new AbstractReceiveListener() {
@Override
protected void onFullBinaryMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
log.log(Level.FINE, "Server received full binary message");
Pooled<ByteBuffer[]> pulledData = message.getData();
try {
ByteBuffer[] resource = pulledData.getResource();
ByteBuffer byteBuffer = WebSockets.mergeBuffers(resource);
String msg = new String(byteBuffer.array());
log.log(Level.FINE, "Sending message to decoder: "+ msg);
writeToDecoder(msg);
}
finally {
pulledData.discard();
}
}
};
webSocketChannel.getReceiveSetter().set(listener);
}
示例5: onFullTextMessage
import io.undertow.websockets.core.WebSocketChannel; //导入依赖的package包/类
/**
* interpret text as js code to execute
*/
@Override
protected void onFullTextMessage(WebSocketChannel socket, BufferedTextMessage message) {
String code = message.getData().trim();
if (code.isEmpty())
return; //ignore
db.runLater(1f, () -> {
JSExec.eval(code, scope, db.js, result -> {
if (result == null)
return;
Object resultObj = result.o;
if (resultObj instanceof Task) {
//if the result of the evaluation is a Task, queue it
start((Task) resultObj);
} else {
//else send the immediate result
sendJSONBinary(socket, result.toJSON(), defaultOutRate, null);
}
});
});
}
示例6: onConnect
import io.undertow.websockets.core.WebSocketChannel; //导入依赖的package包/类
@Override
public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
if (this.hasAuthentication) {
String header = null;
if (exchange.getRequestHeader(Header.COOKIE.toString()) != null) {
header = exchange.getRequestHeader(Header.COOKIE.toString());
}
if (this.requestHelper.hasValidAuthentication(header)) {
channel.getReceiveSetter().set((ChannelListener<? super WebSocketChannel>) Application.getInstance(this.controllerClass));
channel.resumeReceives();
channel.addCloseTask(Application.getInstance(WebSocketCloseListener.class));
Application.getInstance(WebSocketService.class).addChannel(channel);
} else {
IOUtils.closeQuietly(channel);
}
} else {
channel.getReceiveSetter().set((ChannelListener<? super WebSocketChannel>) Application.getInstance(this.controllerClass));
channel.resumeReceives();
channel.addCloseTask(Application.getInstance(WebSocketCloseListener.class));
Application.getInstance(WebSocketService.class).addChannel(channel);
}
}
示例7: registerWebSocketChannelListener
import io.undertow.websockets.core.WebSocketChannel; //导入依赖的package包/类
private void registerWebSocketChannelListener(WebSocketChannel webSocketChannel) {
ChannelListener<WebSocketChannel> listener = new AbstractReceiveListener() {
@Override
protected void onFullBinaryMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
log.trace("Server received full binary message");
Pooled<ByteBuffer[]> pulledData = message.getData();
try {
ByteBuffer[] resource = pulledData.getResource();
ByteBuffer byteBuffer = WebSockets.mergeBuffers(resource);
String msg = new String(byteBuffer.array());
log.trace("Sending message to decoder: {}", msg);
writeToDecoder(msg);
} finally {
pulledData.discard();
}
}
};
webSocketChannel.getReceiveSetter().set(listener);
}
示例8: run
import io.undertow.websockets.core.WebSocketChannel; //导入依赖的package包/类
void run() throws Exception {
CountDownLatch closelatch = new CountDownLatch(2);
server(app.websocket("/ws").onClose(
(c, buf) -> closelatch.countDown()));
stopper = app.listen(port);
CountDownLatch latch = new CountDownLatch(1);
WebSocketChannel channel = WebSocketClient
.connectionBuilder(
worker,
buffer,
new URI(String.format("http://localhost:%d/ws",
port))).connect().get();
channel.addCloseTask(c -> closelatch.countDown());
channel.getReceiveSetter().set(listener(latch));
channel.resumeReceives();
try {
assertion(channel, latch);
} finally {
if (channel.isOpen()) {
channel.sendClose();
closelatch.await(1, TimeUnit.SECONDS);
}
}
}
示例9: processWebSocketRequest
import io.undertow.websockets.core.WebSocketChannel; //导入依赖的package包/类
private void processWebSocketRequest(Request request, Response response) {
HttpServletRequest servletRequest = request.getHttpServletRequest();
HttpServletResponse servletResponse = response.getHttpServletResponse();
WebSocketHttpExchange exchange = new ServletWebSocketHttpExchange(servletRequest, servletResponse, peerConnections);
// exchange.putAttachment(HandshakeUtil.PATH_PARAMS, Collections.<String, String>emptyMap());
Handshake handshake = getHandshake(exchange);
exchange.upgradeChannel((connection, serverExchange) -> {
WebSocketChannel channel = handshake.createChannel(exchange, connection, exchange.getBufferPool());
peerConnections.add(channel);
createWebSocketAdapter(request).onConnect(exchange, channel);
});
handshake.handshake(exchange);
}
示例10: onFullTextMessage
import io.undertow.websockets.core.WebSocketChannel; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void onFullTextMessage() throws Exception {
new MockUnit(Config.class, WebSocketChannel.class, BufferedTextMessage.class, Consumer.class)
.expect(config)
.expect(unit -> {
CountDownLatch ready = unit.mockConstructor(CountDownLatch.class,
new Class[]{int.class }, 1);
ready.await();
})
.expect(unit -> {
BufferedTextMessage msg = unit.get(BufferedTextMessage.class);
expect(msg.getData()).andReturn("x");
Consumer<String> callback = unit.get(Consumer.class);
callback.accept("x");
})
.run(unit -> {
UndertowWebSocket ws = new UndertowWebSocket(unit.get(Config.class));
ws.onTextMessage(unit.get(Consumer.class));
ws.onFullTextMessage(unit.get(WebSocketChannel.class),
unit.get(BufferedTextMessage.class));
});
}
示例11: onCloseMessage
import io.undertow.websockets.core.WebSocketChannel; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void onCloseMessage() throws Exception {
new MockUnit(Config.class, WebSocketChannel.class, CloseMessage.class, BiConsumer.class)
.expect(config)
.expect(unit -> {
CountDownLatch ready = unit.mockConstructor(CountDownLatch.class,
new Class[]{int.class }, 1);
ready.await();
})
.expect(unit -> {
CloseMessage msg = unit.get(CloseMessage.class);
expect(msg.getCode()).andReturn(1000);
expect(msg.getReason()).andReturn(null);
BiConsumer<Integer, Optional<String>> callback = unit.get(BiConsumer.class);
callback.accept(1000, Optional.empty());
})
.run(unit -> {
UndertowWebSocket ws = new UndertowWebSocket(unit.get(Config.class));
ws.onCloseMessage(unit.get(BiConsumer.class));
ws.onCloseMessage(unit.get(CloseMessage.class),
unit.get(WebSocketChannel.class));
});
}
示例12: onError
import io.undertow.websockets.core.WebSocketChannel; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void onError() throws Exception {
Throwable cause = new IllegalStateException("intentional err");
new MockUnit(Config.class, WebSocketChannel.class, CloseMessage.class, Consumer.class)
.expect(config)
.expect(unit -> {
CountDownLatch ready = unit.mockConstructor(CountDownLatch.class,
new Class[]{int.class }, 1);
ready.await();
})
.expect(unit -> {
Consumer<Throwable> callback = unit.get(Consumer.class);
callback.accept(cause);
})
.run(unit -> {
UndertowWebSocket ws = new UndertowWebSocket(unit.get(Config.class));
ws.onErrorMessage(unit.get(Consumer.class));
ws.onError(unit.get(WebSocketChannel.class), cause);
});
}
示例13: resume
import io.undertow.websockets.core.WebSocketChannel; //导入依赖的package包/类
@Test
public void resume() throws Exception {
new MockUnit(Config.class, WebSocketChannel.class, Runnable.class)
.expect(config)
.expect(connect)
.expect(unit -> {
WebSocketChannel ch = unit.get(WebSocketChannel.class);
ch.resumeReceives();
})
.run(unit -> {
UndertowWebSocket ws = new UndertowWebSocket(unit.get(Config.class));
ws.onConnect(unit.get(Runnable.class));
ws.connect(unit.get(WebSocketChannel.class));
ws.resume();
});
}
示例14: isOpen
import io.undertow.websockets.core.WebSocketChannel; //导入依赖的package包/类
@Test
public void isOpen() throws Exception {
new MockUnit(Config.class, WebSocketChannel.class, Runnable.class)
.expect(config)
.expect(connect)
.expect(unit -> {
WebSocketChannel ch = unit.get(WebSocketChannel.class);
expect(ch.isOpen()).andReturn(true);
})
.run(unit -> {
UndertowWebSocket ws = new UndertowWebSocket(unit.get(Config.class));
ws.onConnect(unit.get(Runnable.class));
ws.connect(unit.get(WebSocketChannel.class));
ws.isOpen();
});
}
示例15: pause
import io.undertow.websockets.core.WebSocketChannel; //导入依赖的package包/类
@Test
public void pause() throws Exception {
new MockUnit(Config.class, WebSocketChannel.class, Runnable.class)
.expect(config)
.expect(connect)
.expect(unit -> {
WebSocketChannel ch = unit.get(WebSocketChannel.class);
ch.suspendReceives();
})
.run(unit -> {
UndertowWebSocket ws = new UndertowWebSocket(unit.get(Config.class));
ws.onConnect(unit.get(Runnable.class));
ws.connect(unit.get(WebSocketChannel.class));
ws.pause();
});
}