コード例 #1
0
  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();
  }
コード例 #2
0
  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);
  }