gpt4 book ai didi

org.atmosphere.websocket.WebSocket.resource()方法的使用及代码示例

转载 作者:知者 更新时间:2024-03-27 13:25:05 26 4
gpt4 key购买 nike

本文整理了Java中org.atmosphere.websocket.WebSocket.resource()方法的一些代码示例,展示了WebSocket.resource()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WebSocket.resource()方法的具体详情如下:
包路径:org.atmosphere.websocket.WebSocket
类名称:WebSocket
方法名:resource

WebSocket.resource介绍

[英]Return the an AtmosphereResource used by this WebSocket, or null if the WebSocket has been closed before the WebSocket message has been processed.
[中]返回此WebSocket使用的AtmosphereSource,如果在处理WebSocket消息之前关闭了WebSocket,则返回null。

代码示例

代码示例来源:origin: Atmosphere/atmosphere

@Override
public WebSocket write(AtmosphereResponse r, byte[] data) throws IOException {
  if (data == null) {
    logger.error("Cannot write null value for {}", resource());
    return this;
  }
  return write(r, data, 0, data.length);
}

代码示例来源:origin: Atmosphere/atmosphere

@Override
  public void run() {
    if (WebSocket.class.isAssignableFrom(webSocket.getClass())
        && System.currentTimeMillis() - WebSocket.class.cast(webSocket).lastWriteTimeStampInMilliseconds() > action.timeout()) {
      asynchronousProcessor.endRequest(((AtmosphereResourceImpl) webSocket.resource()), false);
      f.get().cancel(true);
    }
  }
}, action.timeout(), action.timeout(), TimeUnit.MILLISECONDS));

代码示例来源:origin: Atmosphere/atmosphere

@Override
public List<AtmosphereRequest> onMessage(WebSocket webSocket, byte[] data, int offset, int length) {
  logger.trace("broadcast byte");
  byte[] b = new byte[length];
  System.arraycopy(data, offset, b, 0, length);
  webSocket.resource().getBroadcaster().broadcast(b);
  return null;
}

代码示例来源:origin: Atmosphere/atmosphere

@Override
public List<AtmosphereRequest> onMessage(WebSocket webSocket, String data) {
  logger.trace("broadcast String");
  webSocket.resource().getBroadcaster().broadcast(data);
  return null;
}

代码示例来源:origin: Atmosphere/atmosphere

private WebSocketHandlerProxy webSocketHandlerForMessage(WebSocket webSocket) {
  AtmosphereResourceImpl impl = AtmosphereResourceImpl.class.cast(webSocket.resource());
  if (impl != null) {
    impl.getRequest(false).setAttribute(FrameworkConfig.WEBSOCKET_MESSAGE, "true");
    try {
      Utils.inject(impl);
    } catch (IllegalAccessException e) {
      logger.warn("", e);
    }
  }
  return WebSocketHandlerProxy.class.cast(webSocket.webSocketHandler());
}

代码示例来源:origin: Atmosphere/atmosphere

private void onDisconnect(WebSocketEvent event, AtmosphereResourceEventListener l) {
  if (event.webSocket() != null && event.webSocket().resource() != null) {
    framework.notifyDestroyed(event.webSocket().resource().uuid());
  }
  WebSocketEventListener.class.cast(l).onDisconnect(event);
}

代码示例来源:origin: Atmosphere/atmosphere

@Override
public List<AtmosphereRequest> onTextStream(WebSocket webSocket, Reader r) {
  //Converting to a string and delegating to onMessage(WebSocket webSocket, String d) causes issues because the binary data may not be a valid string.
  AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource();
  if (resource == null) {
    logger.trace("The WebSocket has been closed before the message was processed.");
    return null;
  }
  AtmosphereRequest request = resource.getRequest();
  request.setAttribute(FrameworkConfig.WEBSOCKET_SUBPROTOCOL, FrameworkConfig.STREAMING_HTTP_OVER_WEBSOCKET);
  List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
  list.add(constructRequest(webSocket, request.getPathInfo(), request.getRequestURI(), methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).reader(r).build());
  return list;
}

代码示例来源:origin: Atmosphere/atmosphere

@Override
public WebSocket write(AtmosphereResponse r, String data) throws IOException {
  firstWrite.set(true);
  if (data == null) {
    logger.error("Cannot write null value for {}", resource());
    return this;
  }
  if (!isOpen()) throw new IOException("Connection remotely closed for " + uuid);
  logger.trace("WebSocket.write() {}", data);
  boolean transform = !filters.isEmpty() && r.getStatus() < 400;
  if (binaryWrite) {
    byte[] b = data.getBytes(resource().getResponse().getCharacterEncoding());
    if (transform) {
      b = transform(r, b, 0, b.length);
    }
    if (b != null) {
      write(b, 0, b.length);
    }
  } else {
    if (transform) {
      byte[] b = data.getBytes(resource().getResponse().getCharacterEncoding());
      data = new String(transform(r, b, 0, b.length), r.getCharacterEncoding());
    }
    if (data != null) {
      write(data);
    }
  }
  lastWrite = System.currentTimeMillis();
  return this;
}

代码示例来源:origin: Atmosphere/atmosphere

@Override
public List<AtmosphereRequest> onBinaryStream(WebSocket webSocket, InputStream stream) {
  //Converting to a string and delegating to onMessage(WebSocket webSocket, String d) causes issues because the binary data may not be a valid string.
  AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource();
  if (resource == null) {
    logger.trace("The WebSocket has been closed before the message was processed.");
    return null;
  }
  AtmosphereRequest request = resource.getRequest();
  request.setAttribute(FrameworkConfig.WEBSOCKET_SUBPROTOCOL, FrameworkConfig.STREAMING_HTTP_OVER_WEBSOCKET);
  List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
  list.add(constructRequest(webSocket, request.getPathInfo(), request.getRequestURI(), methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).inputStream(stream).build());
  return list;
}

代码示例来源:origin: Atmosphere/atmosphere

firstWrite.set(true);
if (b == null) {
  logger.error("Cannot write null value for {}", resource());
  return this;
if (binaryWrite || resource().forceBinaryWrite()) {
  if (transform) {
    b = transform(r, b, offset, length);

代码示例来源:origin: Atmosphere/atmosphere

@Override
public List<AtmosphereRequest> onMessage(WebSocket webSocket, String message) {
  AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource();
  if (resource == null) {
    logger.trace("The WebSocket has been closed before the message was processed.");

代码示例来源:origin: Atmosphere/atmosphere

@Override
public List<AtmosphereRequest> onMessage(WebSocket webSocket, byte[] d, final int offset, final int length) {
  //Converting to a string and delegating to onMessage(WebSocket webSocket, String d) causes issues because the binary data may not be a valid string.
  AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource();
  if (resource == null) {
    logger.trace("The WebSocket has been closed before the message was processed.");
    return null;
  }
  AtmosphereRequest request = resource.getRequest(false);
  request.setAttribute(FrameworkConfig.WEBSOCKET_SUBPROTOCOL, FrameworkConfig.SIMPLE_HTTP_OVER_WEBSOCKET);
  if (!resource.isInScope()) return Collections.emptyList();
  List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
  list.add(constructRequest(webSocket, request.getPathInfo(), request.getRequestURI(), methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).body(d, offset, length).build());
  return list;
}

代码示例来源:origin: Atmosphere/atmosphere

public void executeClose(WebSocket webSocket, int closeCode) {
  AtmosphereResource r = webSocket.resource();
  boolean isClosedByClient = r == null ? true : r.getAtmosphereResourceEvent().isClosedByClient();
  try {
    if (r != null) {
      asynchronousProcessor.endRequest(AtmosphereResourceImpl.class.cast(r), true);
    }
  } finally {
    if (!isClosedByClient) {
      notifyListener(webSocket, new WebSocketEventListener.WebSocketEvent(closeCode, CLOSE, webSocket));
    }
  }
}

代码示例来源:origin: Atmosphere/atmosphere

private void handleException(Exception ex, WebSocket webSocket, WebSocketHandler webSocketHandler) {
  logger.error("", ex);
  AtmosphereResource r = webSocket.resource();
  if (r != null) {
    webSocketHandler.onError(webSocket, new WebSocketException(ex,
        new AtmosphereResponseImpl.Builder()
            .request(r != null ? AtmosphereResourceImpl.class.cast(r).getRequest(false) : null)
            .status(500)
            .statusMessage("Server Error").build()));
  }
}

代码示例来源:origin: Atmosphere/atmosphere

webSocket.resource().setBroadcaster(AnnotationUtil.broadcaster(framework, p != null ? p.broadcasterClazz : w.broadcasterClazz, path));
} catch (Exception e) {
  logger.error("", e);

代码示例来源:origin: Atmosphere/atmosphere

private void invokeInterceptors(WebSocketHandlerProxy webSocketHandler,
                WebSocket webSocket, Object webSocketMessageAsBody, int offset, int length) throws IOException {
  AtmosphereResourceImpl resource = AtmosphereResourceImpl.class.cast(webSocket.resource());
  if (resource == null) {
    return;

代码示例来源:origin: Atmosphere/atmosphere

private void finish(WebSocket webSocket, AtmosphereResource resource, AtmosphereRequest r, AtmosphereResponse s, boolean closeWebSocket) {
  // Don't take any risk in case something goes wrong and remove the associated resource.
  framework.atmosphereFactory().remove(resource.uuid());
  if (webSocket != null) {
    try {
      r.setAttribute(WebSocket.CLEAN_CLOSE, Boolean.TRUE);
      webSocket.resource(null);
      if (closeWebSocket) webSocket.close(s);
    } catch (IOException e) {
      logger.trace("", e);
    }
  }
  if (r != null) {
    r.destroy(true);
  }
  if (s != null) {
    s.destroy(true);
  }
}

代码示例来源:origin: Atmosphere/atmosphere

@Override
public void notifyListener(WebSocket webSocket, WebSocketEventListener.WebSocketEvent event) {
  AtmosphereResource resource = webSocket.resource();
  if (resource == null) return;

代码示例来源:origin: Atmosphere/atmosphere

final AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource();
        asynchronousProcessor.endRequest(AtmosphereResourceImpl.class.cast(webSocket.resource()), false);

代码示例来源:origin: Atmosphere/atmosphere

protected static AtmosphereRequestImpl.Builder constructRequest(WebSocket webSocket,
                              String pathInfo,
                              String requestURI,
                              String methodType,
                              String contentType,
                              boolean destroyable) {
  AtmosphereResource resource = webSocket.resource();
  AtmosphereRequest request = AtmosphereResourceImpl.class.cast(resource).getRequest(false);
  Map<String, Object> m = attributes(webSocket, request);
  // We need to create a new AtmosphereRequest as WebSocket message may arrive concurrently on the same connection.
  AtmosphereRequestImpl.Builder b = (new AtmosphereRequestImpl.Builder()
      .request(request)
      .method(methodType)
      .contentType(contentType == null ? request.getContentType() : contentType)
      .attributes(m)
      .pathInfo(pathInfo)
      .contextPath(request.getContextPath())
      .servletPath(request.getServletPath())
      .requestURI(requestURI)
      .requestURL(request.requestURL())
      .destroyable(destroyable)
      .headers(request.headersMap())
      .session(resource.session()));
  return b;
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com