Exemplo n.º 1
0
  /**
   * The local endpoint has reached a write failure.
   *
   * <p>A low level I/O failure, or even a jetty side EndPoint close (from idle timeout) are a few
   * scenarios
   *
   * @param t the throwable that caused the write failure
   */
  public void onWriteFailure(Throwable t) {
    ConnectionState event = null;
    synchronized (this) {
      if (this.state == ConnectionState.CLOSED) {
        // already closed
        return;
      }

      // Build out Close Reason
      String reason = "WebSocket Write Failure";
      if (t instanceof EOFException) {
        reason = "WebSocket Write EOF";
        Throwable cause = t.getCause();
        if ((cause != null) && (StringUtil.isNotBlank(cause.getMessage()))) {
          reason = "EOF: " + cause.getMessage();
        }
      } else {
        if (StringUtil.isNotBlank(t.getMessage())) {
          reason = t.getMessage();
        }
      }

      CloseInfo close = new CloseInfo(StatusCode.ABNORMAL, reason);

      finalClose.compareAndSet(null, close);

      this.cleanClose = false;
      this.state = ConnectionState.CLOSED;
      this.inputAvailable = false;
      this.outputAvailable = false;
      this.closeHandshakeSource = CloseHandshakeSource.ABNORMAL;
      event = this.state;
    }
    notifyStateListeners(event);
  }
  public WebSocketSession(URI requestURI, EventDriver websocket, LogicalConnection connection) {
    if (requestURI == null) {
      throw new RuntimeException("Request URI cannot be null");
    }

    this.requestURI = requestURI;
    this.websocket = websocket;
    this.connection = connection;
    this.outgoingHandler = connection;
    this.incomingHandler = websocket;

    // Get the parameter map (use the jetty MultiMap to do this right)
    MultiMap<String> params = new MultiMap<>();
    String query = requestURI.getQuery();
    if (StringUtil.isNotBlank(query)) {
      UrlEncoded.decodeTo(query, params, StringUtil.__UTF8);
    }

    for (String name : params.keySet()) {
      List<String> valueList = params.getValues(name);
      String valueArr[] = new String[valueList.size()];
      valueArr = valueList.toArray(valueArr);
      parameterMap.put(name, valueArr);
    }
  }
Exemplo n.º 3
0
  public ByteBuffer asByteBuffer() {
    if ((statusCode == StatusCode.NO_CLOSE)
        || (statusCode == StatusCode.NO_CODE)
        || (statusCode == (-1))) {
      // codes that are not allowed to be used in endpoint.
      return null;
    }

    ByteBuffer buf = ByteBuffer.allocate(WebSocketFrame.MAX_CONTROL_PAYLOAD);
    buf.putChar((char) statusCode);
    if (StringUtil.isNotBlank(reason)) {
      byte utf[] = StringUtil.getUtf8Bytes(reason);
      buf.put(utf, 0, utf.length);
    }
    BufferUtil.flipToFlush(buf, 0);
    return buf;
  }
Exemplo n.º 4
0
  public void sendStandardRequest() throws IOException {
    StringBuilder req = new StringBuilder();
    req.append("GET /chat HTTP/1.1\r\n");
    req.append("Host: ").append(destHttpURI.getHost());
    if (destHttpURI.getPort() > 0) {
      req.append(':').append(destHttpURI.getPort());
    }
    req.append("\r\n");
    req.append("Upgrade: websocket\r\n");
    req.append("Connection: Upgrade\r\n");
    req.append("Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n");
    req.append("Sec-WebSocket-Origin: ").append(destWebsocketURI.toASCIIString()).append("\r\n");
    if (StringUtil.isNotBlank(protocols)) {
      req.append("Sec-WebSocket-Protocol: ").append(protocols).append("\r\n");
    }

    for (String xtension : extensions) {
      req.append("Sec-WebSocket-Extensions: ").append(xtension).append("\r\n");
    }
    req.append("Sec-WebSocket-Version: ").append(version).append("\r\n");
    req.append("\r\n");
    writeRaw(req.toString());
  }