WebSocketSession接口详解

org.springframework.web.socket

Interface WebSocketSession

所有已知子接口:

NativeWebSocketSession,SocketJsSession

所有已知实现类:

AbstractHttpSocketJsSession,AbstractSockJsSession,AbstractWebSocketSession,JettyWebSocketSession,PollingSockJsSession,StandardWebSocketSession,StreamingSockJsSession,WebSocketServerJsSession


  • public interface WebSocketSession
    一个WebSocket的抽象, 允许通过WebSocket连接发送消息和关闭WebSocket连接.
  • String getId()
    返回唯一的session id
  • URI getUri()
    返回开启WebSocekt连接的URI
  • HttpHeaders getHandshakeHeaders()
    返回握手请求的请求头

HttpHeaders 封装了各种头信息

  • Map getHandshakeAttributes()
    握手请求指定的属性, 如何添加属性查看HandshakeInterceptor
  • Principal getPrincipal()
    返回一个包含认证用户名称的Principal实例, 如果用户没有被认证, 则返回null
  • InetSocketAddress getLocalAddress()
    返回收到请求的(服务器)地址
  • InetSocketAddress getRemoteAddress()
    返回远程客户端地址
  • String getAcceptedProtocol()
    返回协议的子协议, 如果没有指定或者协议不成功则返回null.(先这样翻译, 有更好的私信我)
  • List getExtensions()
    返回协议扩展, 如果没有指定或者协议不成功则返回null.(同上)
  • boolean isOpen()
    返回连接是否打开
  • void sendMessage(Message message) throws IOException
    发送WebSocket消息, 可能是文本, 或者是二进制
  • void close() throws IOException
    关闭WebSocket连接, 状态码1000
  • void close(CloseStatus status) throws IOException
    关闭WebSocket连接, 指定状态码status.

CloseStatus 包含了一大波状态码

参考资料: spring技术文档

你可能感兴趣的:(WebSocketSession接口详解)