683.447d4972.js 7.1 KB
Newer Older
茶陵後's avatar
茶陵後 已提交
1
(window.webpackJsonp=window.webpackJsonp||[]).push([[683],{1114:function(e,t,s){"use strict";s.r(t);var o=s(56),r=Object(o.a)({},(function(){var e=this,t=e.$createElement,s=e._self._c||t;return s("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[s("h1",{attrs:{id:"websocket-整合"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#websocket-整合"}},[e._v("#")]),e._v(" WebSocket 整合")]),e._v(" "),s("p",[e._v("Spring Session 提供了 Spring 的 WebSocket 支持的透明集成。")]),e._v(" "),s("table",[s("thead",[s("tr",[s("th"),e._v(" "),s("th",[e._v("Spring Session 的 WebSocket 支持仅对 Spring 的 WebSocket 支持有效。"),s("br"),e._v("具体来说,它不能直接使用"),s("a",{attrs:{href:"https://www.jcp.org/en/jsr/detail?id=356",target:"_blank",rel:"noopener noreferrer"}},[e._v("JSR-356"),s("OutboundLink")],1),e._v(",因为 JSR-356 没有拦截传入 WebSocket 消息的机制。")])])]),e._v(" "),s("tbody")]),e._v(" "),s("h2",{attrs:{id:"为什么要使用会话和-websockets"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#为什么要使用会话和-websockets"}},[e._v("#")]),e._v(" 为什么要使用会话和 WebSockets?")]),e._v(" "),s("p",[e._v("那么,当我们使用 WebSockets 时,为什么需要 Spring Session 呢?")]),e._v(" "),s("p",[e._v("考虑一个电子邮件应用程序,它的大部分工作都是通过 HTTP 请求完成的。然而,其中还嵌入了一个聊天应用程序,该应用程序可以在 WebSocket API 上工作。如果用户正在积极地与某人聊天,我们不应该超时"),s("code",[e._v("HttpSession")]),e._v(",因为这将是一个非常糟糕的用户体验。然而,这正是"),s("a",{attrs:{href:"https://java.net/jira/browse/WEBSOCKET_SPEC-175",target:"_blank",rel:"noopener noreferrer"}},[e._v("JSR-356"),s("OutboundLink")],1),e._v("所做的。")]),e._v(" "),s("p",[e._v("另一个问题是,根据 JSR-356,如果"),s("code",[e._v("HttpSession")]),e._v("超时,则任何用"),s("code",[e._v("HttpSession")]),e._v("创建的 WebSocket 和经过身份验证的用户都应该被强制关闭。这意味着,如果我们在应用程序中积极地聊天,而不使用 HttpSession,那么我们也会断开与我们的对话的连接。")]),e._v(" "),s("h2",{attrs:{id:"websocket-用法"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#websocket-用法"}},[e._v("#")]),e._v(" WebSocket 用法")]),e._v(" "),s("p",[s("RouterLink",{attrs:{to:"/spring-session/samples.html#samples"}},[e._v(" WebSocket Sample")]),e._v("提供了如何将 Spring Session 与 WebSockets 集成的工作示例。你可以遵循下面几个标题中描述的集成的基本步骤,但我们鼓励你在与自己的应用程序集成时遵循详细的 WebSocket 指南。")],1),e._v(" "),s("h3",{attrs:{id:"httpsession积分"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#httpsession积分"}},[e._v("#")]),e._v(" "),s("code",[e._v("HttpSession")]),e._v("积分")]),e._v(" "),s("p",[e._v("在使用 WebSocket 集成之前,你应该确保首先有["),s("code",[e._v("HttpSession")]),e._v("集成](http-session.html#httpsession)在工作。")]),e._v(" "),s("h4",{attrs:{id:"spring-配置"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#spring-配置"}},[e._v("#")]),e._v(" Spring 配置")]),e._v(" "),s("p",[e._v("在典型的 Spring  WebSocket 应用程序中,你将实现"),s("code",[e._v("WebSocketMessageBrokerConfigurer")]),e._v("。例如,配置可能如下所示:")]),e._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[e._v('@Configuration\n@EnableScheduling\n@EnableWebSocketMessageBroker\npublic class WebSocketConfig implements WebSocketMessageBrokerConfigurer {\n\n\t@Override\n\tpublic void registerStompEndpoints(StompEndpointRegistry registry) {\n\t\tregistry.addEndpoint("/messages").withSockJS();\n\t}\n\n\t@Override\n\tpublic void configureMessageBroker(MessageBrokerRegistry registry) {\n\t\tregistry.enableSimpleBroker("/queue/", "/topic/");\n\t\tregistry.setApplicationDestinationPrefixes("/app");\n\t}\n\n}\n')])])]),s("p",[e._v("我们可以更新配置以使用 Spring Session 的 WebSocket 支持。下面的示例展示了如何做到这一点:")]),e._v(" "),s("p",[e._v("SRC/main/java/samples/config/websocketconfig.java")]),e._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[e._v('@Configuration\n@EnableScheduling\n@EnableWebSocketMessageBroker\npublic class WebSocketConfig extends AbstractSessionWebSocketMessageBrokerConfigurer<Session> { (1)\n\n\t@Override\n\tprotected void configureStompEndpoints(StompEndpointRegistry registry) { (2)\n\t\tregistry.addEndpoint("/messages").withSockJS();\n\t}\n\n\t@Override\n\tpublic void configureMessageBroker(MessageBrokerRegistry registry) {\n\t\tregistry.enableSimpleBroker("/queue/", "/topic/");\n\t\tregistry.setApplicationDestinationPrefixes("/app");\n\t}\n\n}\n')])])]),s("p",[e._v("要连接 Spring Session 支持,我们只需要更改两件事:")]),e._v(" "),s("table",[s("thead",[s("tr",[s("th",[s("strong",[e._v("1")])]),e._v(" "),s("th",[e._v("而不是实现"),s("code",[e._v("WebSocketMessageBrokerConfigurer")]),e._v(",我们扩展"),s("code",[e._v("AbstractSessionWebSocketMessageBrokerConfigurer")])])])]),e._v(" "),s("tbody",[s("tr",[s("td",[s("strong",[e._v("2")])]),e._v(" "),s("td",[e._v("我们将"),s("code",[e._v("registerStompEndpoints")]),e._v("方法重命名为"),s("code",[e._v("configureStompEndpoints")])])])])]),e._v(" "),s("p",[s("code",[e._v("AbstractSessionWebSocketMessageBrokerConfigurer")]),e._v("在幕后做什么?")]),e._v(" "),s("ul",[s("li",[s("p",[s("code",[e._v("WebSocketConnectHandlerDecoratorFactory")]),e._v("作为"),s("code",[e._v("WebSocketHandlerDecoratorFactory")]),e._v("添加到"),s("code",[e._v("WebSocketTransportRegistration")]),e._v("。这确保了一个包含"),s("code",[e._v("WebSocketSession")]),e._v("的自定义"),s("code",[e._v("SessionConnectEvent")]),e._v("被触发。当 Spring Session 结束时,要结束任何 WebSocket 仍处于打开状态的连接,"),s("code",[e._v("WebSocketSession")]),e._v("是必需的。")])]),e._v(" "),s("li",[s("p",[s("code",[e._v("SessionRepositoryMessageInterceptor")]),e._v("作为"),s("code",[e._v("HandshakeInterceptor")]),e._v("添加到每个"),s("code",[e._v("StompWebSocketEndpointRegistration")]),e._v("。这确保将"),s("code",[e._v("Session")]),e._v("添加到 WebSocket 属性中,以允许更新上次访问的时间。")])]),e._v(" "),s("li",[s("p",[s("code",[e._v("SessionRepositoryMessageInterceptor")]),e._v("作为"),s("code",[e._v("ChannelInterceptor")]),e._v("添加到我们的入站"),s("code",[e._v("ChannelRegistration")]),e._v("中。这确保了每次接收入站消息时,都会更新 Spring Session 的最后一次访问时间。")])]),e._v(" "),s("li",[s("p",[s("code",[e._v("WebSocketRegistryListener")]),e._v("被创建为 Spring  Bean。这确保了我们将所有"),s("code",[e._v("Session")]),e._v("ID 映射到相应的 WebSocket 连接。通过维护此映射,我们可以在 Spring Session 结束时关闭所有 WebSocket 连接。")])])])])}),[],!1,null,null,null);t.default=r.exports}}]);