Exemplo n.º 1
0
  protected void connect(URI uri, long timeout, JettyConnection connection) throws Exception {

    if (!getWebsocketClient().isStarted()) websocketClient.start();

    ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.setSubProtocols(WampFactory.getProtocolName());

    Future<Session> future = websocketClient.connect(connection, uri, request);

    if (timeout > 0) future.get(timeout, TimeUnit.MILLISECONDS);
    else future.get();
  }
Exemplo n.º 2
0
  public Session connect(AgentWebSocketClientController controller) throws Exception {
    if (webSocketClient == null || !webSocketClient.isRunning()) {
      if (webSocketClient != null) {
        webSocketClient.stop();
      }
      webSocketClient = builder.build();
      webSocketClient.start();
    }

    LOG.info("Connecting to websocket endpoint: " + urlService.getAgentRemoteWebSocketUrl());
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.addExtensions("fragment;maxLength=" + getMessageBufferSize());
    return webSocketClient
        .connect(controller, new URI(urlService.getAgentRemoteWebSocketUrl()), request)
        .get();
  }
  private void upgradeConnection(ClientUpgradeResponse response) {
    EndPoint endp = getEndPoint();
    Executor executor = getExecutor();
    WebSocketClientConnection connection = new WebSocketClientConnection(endp, executor, client);

    // Initialize / Negotiate Extensions
    EventDriver websocket = client.getWebSocket();
    WebSocketPolicy policy = client.getPolicy();
    String acceptedSubProtocol = response.getAcceptedSubProtocol();

    WebSocketSession session = new WebSocketSession(request.getRequestURI(), websocket, connection);
    session.setPolicy(policy);
    session.setNegotiatedSubprotocol(acceptedSubProtocol);

    connection.setSession(session);
    List<Extension> extensions = client.getFactory().initExtensions(response.getExtensions());

    // Start with default routing.
    IncomingFrames incoming = session;
    // OutgoingFrames outgoing = connection;

    // Connect extensions
    if (extensions != null) {
      connection.getParser().configureFromExtensions(extensions);
      connection.getGenerator().configureFromExtensions(extensions);

      // FIXME
      // Iterator<Extension> extIter;
      // // Connect outgoings
      // extIter = extensions.iterator();
      // while (extIter.hasNext())
      // {
      // Extension ext = extIter.next();
      // ext.setNextOutgoingFrames(outgoing);
      // outgoing = ext;
      // }
      //
      // // Connect incomings
      // Collections.reverse(extensions);
      // extIter = extensions.iterator();
      // while (extIter.hasNext())
      // {
      // Extension ext = extIter.next();
      // ext.setNextIncomingFrames(incoming);
      // incoming = ext;
      // }
    }

    // configure session for outgoing flows
    // session.setOutgoing(outgoing);
    // configure connection for incoming flows
    connection.getParser().setIncomingFramesHandler(incoming);

    // Now swap out the connection
    endp.setConnection(connection);
    connection.onOpen();
  }
  private void validateResponse(ClientUpgradeResponse response) {
    // Check the Accept hash
    String reqKey = request.getKey();
    String expectedHash = AcceptHash.hashKey(reqKey);
    response.validateWebSocketHash(expectedHash);

    // Parse extensions
    List<ExtensionConfig> extensions = new ArrayList<>();
    List<String> extValues = response.getHeaders("Sec-WebSocket-Extensions");
    if (extValues != null) {
      for (String extVal : extValues) {
        QuotedStringTokenizer tok = new QuotedStringTokenizer(extVal, ",");
        while (tok.hasMoreTokens()) {
          extensions.add(ExtensionConfig.parse(tok.nextToken()));
        }
      }
    }
    response.setExtensions(extensions);
  }