gpt4 book ai didi

org.springframework.web.socket.WebSocketHandler类的使用及代码示例

转载 作者:知者 更新时间:2024-03-26 19:51:05 29 4
gpt4 key购买 nike

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

WebSocketHandler介绍

[英]A handler for WebSocket messages and lifecycle events.

Implementations of this interface are encouraged to handle exceptions locally where it makes sense or alternatively let the exception bubble up in which case by default the exception is logged and the session closed with CloseStatus#SERVER_ERROR. The exception handling strategy is provided by org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator and it can be customized or replaced by decorating the WebSocketHandler with a different decorator.
[中]WebSocket消息和生命周期事件的处理程序。
鼓励该接口的实现在合理的情况下在本地处理异常,或者让异常冒泡,在这种情况下,默认情况下会记录异常,并且会话会以CloseStatus#SERVER_错误关闭。异常处理策略由组织提供。springframework。网状物插座汉德勒。例外WebSockEthandlerDecorator,它可以通过使用不同的装饰器装饰WebSocketHandler来定制或替换。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
  this.delegate.afterConnectionClosed(session, closeStatus);
}

代码示例来源:origin: spring-projects/spring-framework

public void delegateConnectionEstablished() throws Exception {
  this.state = State.OPEN;
  this.handler.afterConnectionEstablished(this);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
  this.delegate.handleMessage(session, message);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void connectReceiveAndCloseWithPrelude() throws Exception {
  StringBuilder sb = new StringBuilder(2048);
  for (int i = 0; i < 2048; i++) {
    sb.append('h');
  }
  String body = sb.toString() + "\n" + "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]";
  ClientHttpResponse response = response(HttpStatus.OK, body);
  connect(response);
  verify(this.webSocketHandler).afterConnectionEstablished(any());
  verify(this.webSocketHandler).handleMessage(any(), eq(new TextMessage("foo")));
  verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!")));
  verifyNoMoreInteractions(this.webSocketHandler);
}

代码示例来源:origin: spring-projects/spring-framework

public void delegateError(Throwable ex) throws Exception {
  this.handler.handleTransportError(this, ex);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void errorResponseStatus() throws Exception {
  connect(response(HttpStatus.OK, "o\n"), response(HttpStatus.INTERNAL_SERVER_ERROR, "Oops"));
  verify(this.webSocketHandler).afterConnectionEstablished(any());
  verify(this.webSocketHandler).handleTransportError(any(), any());
  verify(this.webSocketHandler).afterConnectionClosed(any(), any());
  verifyNoMoreInteractions(this.webSocketHandler);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void afterTransportClosed() throws Exception {
  this.session.handleFrame(SockJsFrame.openFrame().getContent());
  this.session.afterTransportClosed(CloseStatus.SERVER_ERROR);
  assertThat(this.session.isOpen(), equalTo(false));
  verify(this.handler).afterConnectionEstablished(this.session);
  verify(this.handler).afterConnectionClosed(this.session, CloseStatus.SERVER_ERROR);
  verifyNoMoreInteractions(this.handler);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void handleFrameMessageWithWebSocketHandlerException() throws Exception {
  this.session.handleFrame(SockJsFrame.openFrame().getContent());
  willThrow(new IllegalStateException("Fake error")).given(this.handler)
      .handleMessage(this.session, new TextMessage("foo"));
  willThrow(new IllegalStateException("Fake error")).given(this.handler)
      .handleMessage(this.session, new TextMessage("bar"));
  this.session.handleFrame(SockJsFrame.messageFrame(CODEC, "foo", "bar").getContent());
  assertThat(this.session.isOpen(), equalTo(true));
  verify(this.handler).afterConnectionEstablished(this.session);
  verify(this.handler).handleMessage(this.session, new TextMessage("foo"));
  verify(this.handler).handleMessage(this.session, new TextMessage("bar"));
  verifyNoMoreInteractions(this.handler);
}

代码示例来源:origin: spring-projects/spring-framework

if (this.handler.supportsPartialMessages()) {
  session.addMessageHandler(new MessageHandler.Partial<String>() {
    @Override
  this.handler.afterConnectionEstablished(this.wsSession);

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean supportsPartialMessages() {
  return this.delegate.supportsPartialMessages();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void delegateMessagesWithErrorAndConnectionClosing() throws Exception {
  WebSocketHandler wsHandler = new ExceptionWebSocketHandlerDecorator(this.webSocketHandler);
  TestSockJsSession sockJsSession = new TestSockJsSession(
      "1", this.sockJsConfig, wsHandler, Collections.<String, Object>emptyMap());
  String msg1 = "message 1";
  String msg2 = "message 2";
  String msg3 = "message 3";
  willThrow(new IOException()).given(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2));
  sockJsSession.delegateConnectionEstablished();
  try {
    sockJsSession.delegateMessages(msg1, msg2, msg3);
    fail("expected exception");
  }
  catch (SockJsMessageDeliveryException ex) {
    assertEquals(Collections.singletonList(msg3), ex.getUndeliveredMessages());
    verify(this.webSocketHandler).afterConnectionEstablished(sockJsSession);
    verify(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg1));
    verify(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2));
    verify(this.webSocketHandler).afterConnectionClosed(sockJsSession, CloseStatus.SERVER_ERROR);
    verifyNoMoreInteractions(this.webSocketHandler);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
  this.delegate.handleTransportError(session, exception);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void responseClosedAfterDisconnected() throws Exception {
  String body = "o\n" + "c[3000,\"Go away!\"]\n" + "a[\"foo\"]\n";
  ClientHttpResponse response = response(HttpStatus.OK, body);
  connect(response);
  verify(this.webSocketHandler).afterConnectionEstablished(any());
  verify(this.webSocketHandler).afterConnectionClosed(any(), any());
  verifyNoMoreInteractions(this.webSocketHandler);
  verify(response).close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void handleFrameMessage() throws Exception {
  this.session.handleFrame(SockJsFrame.openFrame().getContent());
  this.session.handleFrame(SockJsFrame.messageFrame(CODEC, "foo", "bar").getContent());
  verify(this.handler).afterConnectionEstablished(this.session);
  verify(this.handler).handleMessage(this.session, new TextMessage("foo"));
  verify(this.handler).handleMessage(this.session, new TextMessage("bar"));
  verifyNoMoreInteractions(this.handler);
}

代码示例来源:origin: org.springframework/spring-websocket

if (this.handler.supportsPartialMessages()) {
  session.addMessageHandler(new MessageHandler.Partial<String>() {
    @Override
  this.handler.afterConnectionEstablished(this.wsSession);

代码示例来源:origin: org.springframework/spring-websocket

@Override
public boolean supportsPartialMessages() {
  return this.delegate.supportsPartialMessages();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void connectReceiveAndClose() throws Exception {
  String body = "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]";
  ClientHttpResponse response = response(HttpStatus.OK, body);
  connect(response);
  verify(this.webSocketHandler).afterConnectionEstablished(any());
  verify(this.webSocketHandler).handleMessage(any(), eq(new TextMessage("foo")));
  verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!")));
  verifyNoMoreInteractions(this.webSocketHandler);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) {
  try {
    getDelegate().afterConnectionClosed(session, closeStatus);
  }
  catch (Throwable ex) {
    if (logger.isWarnEnabled()) {
      logger.warn("Unhandled exception after connection closed for " + this, ex);
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

public void handleTransportError(Throwable error) {
  try {
    if (logger.isErrorEnabled()) {
      logger.error("Transport error in " + this, error);
    }
    this.webSocketHandler.handleTransportError(this, error);
  }
  catch (Throwable ex) {
    logger.error("WebSocketHandler.handleTransportError threw an exception", ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
  this.delegate.afterConnectionEstablished(session);
}

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