use of com.neovisionaries.ws.client.WebSocket in project ninja by ninjaframework.
the class WebSocketTest method websocketsEnabled.
@Test
public void websocketsEnabled() throws Exception {
// this test is not really specific to jetty, but its easier to test here
NinjaJetty standalone = new NinjaJetty().externalConfigurationPath("conf/jetty.com.session.conf").port(RANDOM_PORT);
try {
standalone.start();
String url = "ws://localhost:" + RANDOM_PORT + "/example";
WebSocket ws = new WebSocketFactory().createSocket(url);
try {
ws.connect();
assertThat(ws.isOpen(), is(true));
} finally {
ws.disconnect(WebSocketCloseCode.NORMAL);
}
} finally {
standalone.shutdown();
}
}
use of com.neovisionaries.ws.client.WebSocket in project Javacord by BtoBastian.
the class DiscordWebSocketAdapter method onDisconnected.
@Override
public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) {
Optional<WebSocketFrame> closeFrameOptional = Optional.ofNullable(closedByServer ? serverCloseFrame : clientCloseFrame);
String closeReason = closeFrameOptional.map(WebSocketFrame::getCloseReason).orElse("unknown");
String closeCodeString = closeFrameOptional.map(closeFrame -> {
int code = closeFrame.getCloseCode();
return WebSocketCloseCode.fromCode(code).map(closeCode -> closeCode + " (" + code + ")").orElseGet(() -> String.valueOf(code));
}).orElse("'unknown'");
logger.info("Websocket closed with reason '{}' and code {} by {}!", closeReason, closeCodeString, closedByServer ? "server" : "client");
LostConnectionEvent lostConnectionEvent = new LostConnectionEventImpl(api);
api.getEventDispatcher().dispatchLostConnectionEvent(null, lostConnectionEvent);
// Squash it, until it stops beating
heart.squash();
if (!ready.isDone()) {
ready.complete(false);
return;
}
// Reconnect
if (reconnect) {
reconnectingOrResumingLock.lock();
try {
reconnectAttempt.incrementAndGet();
} finally {
reconnectingOrResumingLock.unlock();
}
logger.info("Trying to reconnect/resume in {} seconds!", api.getReconnectDelay(reconnectAttempt.get()));
// Reconnect after a (short?) delay depending on the amount of reconnect attempts
api.getThreadPool().getScheduler().schedule(this::connect, api.getReconnectDelay(reconnectAttempt.get()), TimeUnit.SECONDS);
}
}
Aggregations