org.kurento.jsonrpc.client.JsonRpcClientWebSocket Java Examples

The following examples show how to use org.kurento.jsonrpc.client.JsonRpcClientWebSocket. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OpenViduClient.java    From openvidu with Apache License 2.0 5 votes vote down vote up
public OpenViduClient(String wsUri) {
  this(new JsonRpcClientWebSocket(wsUri, new JsonRpcWSConnectionListener() {

    @Override
    public void reconnected(boolean sameServer) {
    }

    @Override
    public void disconnected() {
      log.warn("JsonRpcWebsocket connection: Disconnected");
    }

    @Override
    public void connectionFailed() {
      log.warn("JsonRpcWebsocket connection: Connection failed");
    }

    @Override
    public void connected() {
    }

    @Override
    public void reconnecting() {
      log.warn("JsonRpcWebsocket connection: is reconnecting");
    }
  }, new SslContextFactory(true)));
}
 
Example #2
Source File: KurentoRoomClient.java    From kurento-room with Apache License 2.0 5 votes vote down vote up
public KurentoRoomClient(String wsUri) {
  this(new JsonRpcClientWebSocket(wsUri, new JsonRpcWSConnectionListener() {

    @Override
    public void reconnected(boolean sameServer) {
    }

    @Override
    public void disconnected() {
      log.warn("JsonRpcWebsocket connection: Disconnected");
    }

    @Override
    public void connectionFailed() {
      log.warn("JsonRpcWebsocket connection: Connection failed");
    }

    @Override
    public void connected() {
    }

    @Override
    public void reconnecting() {
      log.warn("JsonRpcWebsocket connection: is reconnecting");
    }
  }, new SslContextFactory(true)));
}
 
Example #3
Source File: ReconnectionTest.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Test
public void givenClientWithHeartbeat_whenWaitMoreThanIdleTimeout_thenClientIsNotDisconnected()
    throws IOException, InterruptedException {

  final EventWaiter reconnecting = new EventWaiter("reconnecting");
  final EventWaiter reconnected = new EventWaiter("reconnected");

  JsonRpcWSConnectionAdapter listener = new JsonRpcWSConnectionAdapter() {
    @Override
    public void reconnecting() {
      reconnecting.eventReceived();
    }

    @Override
    public void reconnected(boolean sameServer) {
      reconnected.eventReceived();
    }
  };

  try (JsonRpcClientWebSocket client = new JsonRpcClientWebSocket(
      "ws://localhost:" + getPort() + "/reconnection", listener)) {

    client.setIdleTimeout(5000);
    client.enableHeartbeat(4000);

    for (int i = 0; i < 5; i++) {
      client.sendRequest("sessiontest", String.class);
      Thread.sleep(10000);
    }
  }

  assertThat(reconnecting.isEventRecived()).as("Event reconnecting received").isEqualTo(false);
  assertThat(reconnecting.isEventRecived()).as("Event reconnected received").isEqualTo(false);
}
 
Example #4
Source File: CloseMessageTest.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws IOException, InterruptedException {

  JsonRpcClientWebSocket client = (JsonRpcClientWebSocket) createJsonRpcClient("/reconnection");
  client.setSendCloseMessage(true);

  Assert.assertEquals("new", client.sendRequest("sessiontest", String.class));
  Assert.assertEquals("old", client.sendRequest("sessiontest", String.class));
  Assert.assertEquals("old", client.sendRequest("sessiontest", String.class));

  String sessionId = client.getSession().getSessionId();

  client.close();

  JsonRpcClient client2 = createJsonRpcClient("/reconnection");
  client2.connect();
  client2.setSessionId(sessionId);

  Assert.assertEquals("new", client2.sendRequest("sessiontest", String.class));
  Assert.assertEquals("old", client2.sendRequest("sessiontest", String.class));

}
 
Example #5
Source File: ReconnectionTest.java    From kurento-java with Apache License 2.0 3 votes vote down vote up
@Test
public void givenReconnectedSession_whenSessionIdIsRemovedFromClient_thenServerUsesWebSocketAsSessionIdSource()
    throws IOException, InterruptedException {

  try (JsonRpcClientWebSocket client = new JsonRpcClientWebSocket(
      "ws://localhost:" + getPort() + "/reconnection")) {

    assertThat(client.sendRequest("sessiontest", String.class)).isEqualTo("new");
    assertThat(client.sendRequest("sessiontest", String.class)).isEqualTo("old");
    assertThat(client.sendRequest("sessiontest", String.class)).isEqualTo("old");

    String sessionId = client.getSession().getSessionId();

    client.closeNativeClient();

    // Wait for reconnection
    Thread.sleep(100);

    assertThat(client.sendRequest("sessiontest", String.class)).isEqualTo("old");
    assertThat(client.sendRequest("sessiontest", String.class)).isEqualTo("old");

    client.setSessionId(null);

    assertThat(client.sendRequest("sessiontest", String.class)).isEqualTo("old");
    assertThat(client.sendRequest("sessiontest", String.class)).isEqualTo("old");

    assertThat(client.getSession().getSessionId()).isEqualTo(sessionId);

  }
}
 
Example #6
Source File: ReconnectionTest.java    From kurento-java with Apache License 2.0 2 votes vote down vote up
@Test
public void givenJsonRpcClientAndServer_whenServerIsDown_thenClientKeepsReconnectingUntilServerIsUpAgain()
    throws Exception {

  final EventWaiter reconnecting = new EventWaiter("reconnecting");
  final EventWaiter reconnected = new EventWaiter("reconnected");

  JsonRpcWSConnectionAdapter listener = new JsonRpcWSConnectionAdapter() {
    @Override
    public void reconnecting() {
      reconnecting.eventReceived();
    }

    @Override
    public void reconnected(boolean sameServer) {
      reconnected.eventReceived();
    }
  };

  try (JsonRpcClientWebSocket client = new JsonRpcClientWebSocket(
      "ws://localhost:" + getPort() + "/reconnection", listener)) {

    client.setTryReconnectingForever(true);
    client.enableHeartbeat(2000);

    Thread.sleep(1000);

    client.connect();

    log.debug("--------> Client connected to server");

    server.close();

    log.debug("--------> Server closed");

    reconnecting.waitFor(3000);

    log.debug("--------> Event reconnecting received in client");

    assertThat(reconnected.isEventRecived()).isFalse();

    // Wait some time to verify client is reconnecting
    Thread.sleep(20000);

    assertThat(reconnected.isEventRecived()).isFalse();

    log.debug("--------> Starting new server after 20s");

    startServer();

    log.debug("--------> New server started");

    log.debug("--------> Waiting 10s to client reconnection");

    reconnected.waitFor(10000);

    log.debug("--------> Client reconnected event received");

  }
}
 
Example #7
Source File: ReconnectionTest.java    From kurento-java with Apache License 2.0 2 votes vote down vote up
@Test
public void givenJsonRpcClientAndServer_whenServerIsDown_thenClientKeepsReconnectingUntilMaxTime()
    throws Exception {

  final EventWaiter reconnecting = new EventWaiter("reconnecting");
  final EventWaiter disconnected = new EventWaiter("disconnected");

  JsonRpcWSConnectionAdapter listener = new JsonRpcWSConnectionAdapter() {
    @Override
    public void reconnecting() {
      reconnecting.eventReceived();
    }

    @Override
    public void disconnected() {
      disconnected.eventReceived();
    }
  };

  try (JsonRpcClientWebSocket client = new JsonRpcClientWebSocket(
      "ws://localhost:" + getPort() + "/reconnection", listener)) {

    client.setTryReconnectingMaxTime(7000);
    client.enableHeartbeat(2000);

    Thread.sleep(1000);

    client.connect();

    log.debug("--------> Client connected to server");

    server.close();

    log.debug("--------> Server closed");

    reconnecting.waitFor(3000);

    log.debug("--------> Event reconnecting received in client");

    assertThat(disconnected.isEventRecived()).isFalse();

    Thread.sleep(7000);

    disconnected.waitFor(20000);

  } finally {
    startServer();
  }
}
 
Example #8
Source File: ReconnectionTest.java    From kurento-java with Apache License 2.0 2 votes vote down vote up
@Test
public void givenReconnectingClient_whenClientIsClosed_thenClientIsNotReconnectedWhenServerIsUpAgain()
    throws Exception {

  final EventWaiter reconnecting = new EventWaiter("reconnecting");
  final EventWaiter reconnected = new EventWaiter("reconnected");
  final EventWaiter disconnected = new EventWaiter("disconnected");

  JsonRpcWSConnectionAdapter listener = new JsonRpcWSConnectionAdapter() {
    @Override
    public void reconnecting() {
      reconnecting.eventReceived();
    }

    @Override
    public void reconnected(boolean sameServer) {
      reconnected.eventReceived();
    }

    @Override
    public void disconnected() {
      disconnected.eventReceived();
    }
  };

  try (JsonRpcClientWebSocket client = new JsonRpcClientWebSocket(
      "ws://localhost:" + getPort() + "/reconnection", listener)) {

    client.setTryReconnectingForever(true);
    client.enableHeartbeat(2000);

    Thread.sleep(1000);

    client.connect();

    log.debug("--------> Client connected to server");

    server.close();

    log.debug("--------> Server closed");

    reconnecting.waitFor(3000);

    log.debug("--------> Event reconnecting received in client");

    assertThat(reconnected.isEventRecived()).isFalse();

    // Wait some time to verify client is reconnecting
    Thread.sleep(20000);

    assertThat(reconnected.isEventRecived()).isFalse();

    log.debug("--------> Starting new server after 20s");

    client.close();

    disconnected.waitFor(20000);

    log.debug("--------> Client is disconnected");

    startServer();

    log.debug("--------> New server started");

    Thread.sleep(10000);

    assertThat(reconnected.isEventRecived()).isFalse();

    log.debug("--------> Client is not reconnected after 10s after closing it");

  }
}
 
Example #9
Source File: ReconnectionFromServerTest.java    From kurento-java with Apache License 2.0 2 votes vote down vote up
@Test
public void test() throws IOException, InterruptedException {

  JsonRpcClient client = new JsonRpcClientWebSocket(
      "ws://localhost:" + getPort() + "/reconnection2");
  client.setServerRequestHandler(new DefaultJsonRpcHandler<JsonElement>() {

    @Override
    public void handleRequest(Transaction transaction, Request<JsonElement> request)
        throws Exception {

      log.debug("Receive request in client: " + request);
      transaction.sendResponse("world");
      log.debug("Response sent from client");
    }
  });

  Assert.assertEquals("new", client.sendRequest("sessiontest", String.class));

  waitForServer();

  Assert.assertEquals("old", client.sendRequest("sessiontest", String.class));

  waitForServer();

  log.debug("SessionId: " + client.getSession().getSessionId());

  JsonRpcClientWebSocket webSocketClient = (JsonRpcClientWebSocket) client;

  webSocketClient.closeNativeClient();

  Thread.sleep(100);

  Assert.assertEquals("old", client.sendRequest("sessiontest", String.class));

  waitForServer();

  log.debug("Acquired");

  client.close();

}