Exemplo n.º 1
0
  @Override
  public <C> void output(C context, Callback<C> callback, WebSocketFrame frame) throws IOException {
    ByteBuffer buf = generator.generate(frame);
    if (LOG.isDebugEnabled()) {
      LOG.debug("writing out: {}", BufferUtil.toDetailString(buf));
    }
    BufferUtil.writeTo(buf, out);
    out.flush();

    if (frame.getOpCode() == OpCode.CLOSE) {
      disconnect();
    }
  }
Exemplo n.º 2
0
  public String expectUpgradeResponse() throws IOException {
    String respHeader = readResponseHeader();

    if (LOG.isDebugEnabled()) {
      LOG.debug("Response Header: {}{}", '\n', respHeader);
    }

    Assert.assertThat("Response Code", respHeader, startsWith("HTTP/1.1 101 Switching Protocols"));
    Assert.assertThat(
        "Response Header Upgrade", respHeader, containsString("Upgrade: WebSocket\r\n"));
    Assert.assertThat(
        "Response Header Connection", respHeader, containsString("Connection: Upgrade\r\n"));

    // collect extensions configured in response header
    List<Extension> extensions = getExtensions(respHeader);

    // Start with default routing
    incoming = this;
    outgoing = this;

    // Connect extensions
    if (extensions != null) {
      generator.configureFromExtensions(extensions);
      parser.configureFromExtensions(extensions);

      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 parser
    parser.setIncomingFramesHandler(incoming);

    return respHeader;
  }