gpt4 book ai didi

org.atmosphere.websocket.WebSocket类的使用及代码示例

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

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

WebSocket介绍

[英]Represent a portable WebSocket implementation which can be used to write message.
[中]表示可用于编写消息的可移植WebSocket实现。

代码示例

代码示例来源: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 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 void close(AtmosphereResponse r) throws IOException {
  logger.trace("WebSocket.close() for {}", uuid);
  try {
    // Never trust underlying server.
    // https://github.com/Atmosphere/atmosphere/issues/1633
    if (r.request() != null && r.request().getAttribute(CLEAN_CLOSE) == null) {
      close();
    }
  } catch (Exception ex) {
    logger.trace("", ex);
  }
  try {
    bb.clear();
    cb.clear();
    // NOTE #1961 if the buffer is cached at thread-local, it needs to be released here.
  } catch (Exception ex) {
    logger.trace("", ex);
  }
}

代码示例来源: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

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

/**
 * Use the underlying container's websocket to write the byte.
 *
 * @param b      a websocket byte message
 * @return this
 * @throws IOException
 */
public WebSocket write(byte[] b) throws IOException {
  return write(b, 0, b.length);
}

代码示例来源:origin: com.wordnik/swaggersocket

AtmosphereResource resource = webSocket.resource();
    webSocket.write(resource.getResponse(), mapper.writeValueAsBytes(statusMessage));
    webSocket.webSocketResponseFilter(serializer);
      StatusMessage statusMessage = new StatusMessage.Builder().status(new StatusMessage.Status(503, "Not Allowed"))
          .identity(identity).build();
      webSocket.write(resource.getResponse(), mapper.writeValueAsBytes(statusMessage));
      return null;

代码示例来源: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 close() throws IOException {
  event.setCloseByApplication(true);
  notifyListeners();
  cancel();
  if (webSocket != null && webSocket.isOpen()) {
    webSocket.close();
  }
}

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

webSocket.resource(r);
webSocketProtocol.onOpen(webSocket);
WebSocketHandler proxy = null;
  webSocket.webSocketHandler(proxy).resource().suspend(-1);
  proxy.onOpen(webSocket);
if (webSocket.resource() != null) {
  final Action action = ((AtmosphereResourceImpl) webSocket.resource()).action();
  if (action.timeout() != -1 && !framework.getAsyncSupport().getContainerName().contains("Netty")) {
    final AtomicReference<Future<?>> f = new AtomicReference();
  AsynchronousProcessor.class.cast(framework.getAsyncSupport()).completeLifecycle(r, true);
webSocket.shiftAttributes();

代码示例来源:origin: motown-io/motown

public boolean isOpen() {
  return webSocket != null && webSocket.isOpen();
}

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

private static Map<String, Object> attributes(WebSocket webSocket, AtmosphereRequest request) {
    Map<String, Object> m = new ConcurrentHashMap<String, Object>();
    m.putAll(webSocket.attributes());
    return m;
  }
}

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

protected byte[] transform(AtmosphereResponse response, byte[] b, int offset, int length) throws IOException {
  AsyncIOWriter a = response.getAsyncIOWriter();
  // NOTE #1961 for now, create a new buffer par transform call and release it after the transform call.
  //      Alternatively, we may cache the buffer in thread-local and use it while this thread invokes
  //      multiple writes and release it when this thread invokes the close method.
  ByteArrayAsyncWriter buffer = new ByteArrayAsyncWriter();
  try {
    response.asyncIOWriter(buffer);
    invokeInterceptor(response, b, offset, length);
    return buffer.stream().toByteArray();
  } finally {
    buffer.close(null);
    response.asyncIOWriter(a);
  }
}

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

@Override
public void onConnect(WebSocketEvent event) {
  logger.trace("calling from " + this.getClass().getName() + " : " + "onConnect");
  sessionWrapper.setWebSocket(event.webSocket());
  try {
    event.webSocket().write(new SocketIOPacketImpl(PacketType.CONNECT).toString());
  } catch (IOException e) {
    e.printStackTrace();
    sessionWrapper.getSession().onShutdown();
  }
  try {
    sessionWrapper.getSession().setAtmosphereResourceImpl((AtmosphereResourceImpl) event.webSocket().resource());
    sessionWrapper.getSession().onConnect(sessionWrapper.getSession().getAtmosphereResourceImpl(), sessionWrapper);
    sessionWrapper.initiated(true);
  } catch (Exception e) {
    logger.error(e.getMessage(),e);
    sessionWrapper.getSession().onShutdown();
  }
}

代码示例来源:origin: org.apache.camel/camel-atmosphere-websocket

@Override
  public void run() {
    try {
      if (message instanceof String) {
        websocket.write((String) message);
      } else if (message instanceof byte[]) {
        websocket.write((byte[]) message, 0, ((byte[]) message).length);
      } else {
        // this should not happen unless one of the supported types is missing above.
        LOG.warn("unexpected message type {}", message.getClass());
      }
    } catch (Exception e) {
      LOG.error("Error when writing to websocket", e);
    }
  }
});

代码示例来源:origin: motown-io/motown

public void close() {
  if (webSocket != null && webSocket.isOpen()) {
    webSocket.close();
  }
}

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

@Override
public void close(final WebSocket webSocket, int closeCode) {
  WebSocketHandler webSocketHandler = webSocket.webSocketHandler();
  final AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource();
          asynchronousProcessor.endRequest(AtmosphereResourceImpl.class.cast(webSocket.resource()), false);

代码示例来源: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

firstWrite.set(true);
if (b == null) {
  logger.error("Cannot write null value for {}", resource());
  return this;
if (!isOpen()) throw new IOException("Connection remotely closed for " + uuid);
if (binaryWrite || resource().forceBinaryWrite()) {
  if (transform) {
    b = transform(r, b, offset, length);
    write(b, 0, b.length);
  String charset = r.getCharacterEncoding() == null ? "UTF-8" : r.getCharacterEncoding();
  if (transform) {
    data = new String(transform(r, b, offset, length), charset);
  } else {
    data = new String(b, offset, length, charset);
    write(data);

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