Skip to content

Commit dbd2040

Browse files
committed
test HttpSessionHandshakeInterceptor
1 parent 42fcad4 commit dbd2040

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

src/main/java/cn/netbuffer/springboot/websocket/demo/config/SpringWebSocketConfig.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.springframework.web.socket.WebSocketHandler;
99
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
1010
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
11+
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
1112

1213
@Slf4j
1314
@Configuration
@@ -16,7 +17,10 @@ public class SpringWebSocketConfig implements WebSocketConfigurer {
1617
@Override
1718
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
1819
// registry.addHandler(buildTextHandler(), "/ws").setAllowedOrigins("*");
19-
registry.addHandler(buildMessageHandler(), "/ws").setAllowedOrigins("*");
20+
registry.addHandler(buildMessageHandler(), "/ws")
21+
//复制HttpSession中的属性到WebSocketSession中
22+
.addInterceptors(new HttpSessionHandshakeInterceptor())
23+
.setAllowedOrigins("*");
2024
}
2125

2226
@Bean
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cn.netbuffer.springboot.websocket.demo.controller;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.web.bind.annotation.PostMapping;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
import javax.servlet.http.HttpSession;
8+
9+
@Slf4j
10+
@RestController
11+
@RequestMapping("/http/session")
12+
public class HttpSessionController {
13+
14+
@PostMapping("set")
15+
public void setSessionValue(HttpSession httpSession, String key, String value) {
16+
httpSession.setAttribute(key, value);
17+
log.debug("setSessionValue key={} value={}", key, value);
18+
}
19+
20+
}

src/main/java/cn/netbuffer/springboot/websocket/demo/websocket/handler/MessageHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public void afterConnectionEstablished(WebSocketSession session) throws Exceptio
1212
super.afterConnectionEstablished(session);
1313
SessionManager.put(session.getId(), session);
1414
log.info("session[{}] afterConnectionEstablished", session.getId());
15+
log.info("get WebSocketSession attributes={}",session.getAttributes());
1516
}
1617

1718
@Override

src/main/resources/templates/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@
2323
alert("send success");
2424
});
2525
}
26+
27+
function set_session() {
28+
var key = prompt("请输入key");
29+
var value = prompt("请输入value");
30+
fetch("/http/session/set", {
31+
body: ({"key": key, "value": value}),
32+
method: 'POST'
33+
})
34+
.then(function (response) {
35+
return response;
36+
})
37+
.then(function (response) {
38+
console.log(`send msg success`);
39+
alert("send success");
40+
});
41+
}
2642
</script>
2743
</body>
2844
</html>

0 commit comments

Comments
 (0)