본문 바로가기
ETC/Spring

[Spring ]WebSocketSession 에서 HttpSession 값 사용하는 방법

by Gnaseel 2020. 1. 12.
728x90
반응형

같은 세션이라고 해도 HttpSession에 값이 있다고 해서 WebSocketSession에 그대로 그 값이 전달되지 않는다.

 

그래서 우리는 client가 Server에 handshake을 진행할 때

HttpSession에 있는 attribute들을 WebSocketSession에 전달하도록 따로 설정해야한다.

 

혹여 handshake라는 단어가 낯설다면...

 

<websocket:handlers>
		<websocket:mapping handler="당신의 handler 이름" path="당신의 path" />
		<websocket:handshake-interceptors>
			<beans:bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor" />
		</websocket:handshake-interceptors>
		<websocket:sockjs/>
</websocket:handlers>

다음과 같다.

servlet-context의 websocket에 handshake-interceptors라는 구문을 추가해주면 되는데

(interceptor는 http통신에서 request, response를 가로채는 역할을 한다.)

Httpsession에 있는 값을 가로채서 WebSocketSession에 똑같이 넣어주는 역할을 한다.

 

이로서 WebSocketSession의 attribute에 우리가 원하는 값을 넣어줄 수 있고,

이 값을 활용하면 모든 session에 대한 전체 채팅이 아닌 특정 session에 대한 1:1채팅 또는 알림 기능을 구현할 수 있다.

 

반응형

'ETC > Spring' 카테고리의 다른 글

[Web Socket / Spring] 실시간 채팅 기능 구현  (23) 2020.01.12
Spring과 Oracle 연동하기  (0) 2019.11.25