โโ ์น์์ผโโ
- Thymeleaf, Websocket
- HttpSession ์ ๋ฌ(Controller -> WebSocket)
- Interceptor ๋ฐฉ์์ผ๋ก ServletContext ์ ๋ฌ
- page, request, session, application
- application : ์น์๋ฒ์ ํ๊ฐ๋ง ์กด์ฌ
- application = ServletContext
- Controller์์ ServletContext ์ HttpSession์ ์ฐธ์กฐ๋ฅผ ์ ์ฅ
- ์ธํฐ์
ํฐ๋ฅผ ํตํด ServletContext ๋ฅผ WebSocket์ผ๋ก ์ ๋ฌ
- WebSocket์์๋ WebSocketSession์ ํตํด์ ์ ๋ฌ๋ ์์ฑ ๊ฐ์ ์ถ์ถํ ์ ์๋ค
- WebSocket์์๋ ๋จผ์ ServletContext๋ฅผ ์ถ์ถํ๊ณ ServletContext๋ฅผ ํตํด
HttpSession๊ฐ์ฒด๋ฅผ ์ถ์ถํ ์ ์๋ค
applicaation < jsp์์ ๋ถ๋ฅด๋ ๋ง (์์ญ ๊ฐ์ฒด) ์๋ฒ์์ ServletContext๋ผ๊ณ ๋ถ๋ฅธ๋ค.
โโ์ ์ฒด ์ฑํ ๋ฐฉ ๋ง๋ค๊ธฐโโ
WebSocketConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Configuration
@EnableWebSocket
public class WebSocketConfiguration implements WebSocketConfigurer
{
private final static String CHAT_ENDPOINT="/ws/chat";
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(getChatWebSocketHandler(), CHAT_ENDPOINT)
.addInterceptors(new HttpHandshakeInterceptor())
.setAllowedOrigins("*");
// ์์์ ์ง์ ํ ์ธํฐ์
ํฐ๋ฅผ ํตํด WebSocketHandler์๊ฒ ํ์ํ ์์ฑ๋ค์ ์ ๋ฌํ ์ ์๋ค
log.info("์น์์ผ ํธ๋ค๋ฌ ๋ฑ๋ก ์๋ฃ");
}
@Bean
public WebSocketHandler getChatWebSocketHandler() {
return new WebSocketHandler();
}
}
|
cs |
WSChatController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Controller
@RequestMapping("/ws")
public class WSChatController
{
@GetMapping("/")
public String index(HttpServletRequest req)
{
return "thymeleaf/index";
}
@GetMapping("/in") // localhost/ws/in?userid=smith ํ์์ผ๋ก ์์ฒญํ์ฌ ๋ก๊ทธ์ธ ํ
์คํธ
public String chatForm(HttpServletRequest request, @RequestParam("userid")String userid, Model model)
{
model.addAttribute("userid", userid);
HttpSession session = request.getSession();
log.info("์์ฑ๋ ์ธ์
ID={}", session.getId());
//HttpSession์ ServletContext ์์ญ์ ์ ์ฅํ๋ฉด ์ธํฐ์
ํฐ๋ฅผ ๊ฑฐ์ณ WebSocketHandler์๊ฒ ์ ๋ฌ๋จ
request.getServletContext().setAttribute("httpSession", session);
session.setAttribute("userid", userid);
log.info("๋ก๊ทธ์ธ ์ฑ๊ณต({}), session={}", userid, session.getId());
return "thymeleaf/chat";
}
}
|
cs |
HttpHandshakeInterceptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import java.util.Map;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.server.HandshakeInterceptor;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class HttpHandshakeInterceptor implements HandshakeInterceptor
{
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
org.springframework.web.socket.WebSocketHandler wsHandler, Map<String, Object> attributes)
throws Exception {
if (request instanceof ServletServerHttpRequest) {
//ํ๋ผ๋ฏธํฐ๋ก ์ ๋ฌ๋ attributes ์ ์์ฑ์ ์ค์ ํด์ฃผ๋ฉด WebSocketHandler์์ WebSocketSession์ผ๋ก ์ ๋ฌ๋จ
Object obj = ((ServletServerHttpRequest) request).getServletRequest().getServletContext().getAttribute("httpSession");
HttpSession session = (HttpSession) obj;
attributes.put("httpSession", session); // WebSocketHandler์๊ฒ HttpSession ์ ๋ฌ
log.info("์ธํฐ์
ํฐ, ์ธ์
ID={}", session.getId());
}
return true;
}
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
org.springframework.web.socket.WebSocketHandler wsHandler, Exception exception) {
// TODO Auto-generated method stub
}
}
|
cs |
WebSocketHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import jakarta.servlet.http.HttpSession;
import jakarta.websocket.OnOpen;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class WebSocketHandler extends TextWebSocketHandler
{
private final List<WebSocketSession> webSocketSessions = new ArrayList<>();
@Override /* ํด๋ผ์ด์ธํธ ์ ์์์ ํธ์ถ๋จ */
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
webSocketSessions.add(session);
System.out.println("Client Connected");
//์ธํฐ์
ํฐ์ ์ํด HttpSession์ ์์ฑ์ ์๋์ฒ๋ผ WebSocketSession๊ฐ์ฒด๋ก ์ ๋ฌ๋์ด ์ฌ๊ธฐ์ ๋ค๋ฃฐ ์ ์๋ค
HttpSession httpSession = (HttpSession)session.getAttributes().get("httpSession");
log.info("์น์์ผํธ๋ค๋ฌ, httpSession={}", httpSession.getId()); // ์ปจํธ๋กค๋ฌ์ ์ธ์
id์ ๋์ผํ์ง ํ์ธ
String userid = (String)httpSession.getAttribute("userid");
log.info("์น์์ผํธ๋ค๋ฌ, userid={}", userid);
}
@Override /* ์๋ฒ์ ๋ฉ์์ง ๋์ฐฉ์ ํธ์ถ๋จ */
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
System.out.println("Text Message:"+ message);
for(WebSocketSession webSocketSession: webSocketSessions) {
webSocketSession.sendMessage(message);
}
}
@Override /* ์ ์ ํด์ ์ ํธ์ถ๋จ */
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
System.out.println("Connection Closed");
webSocketSessions.remove(session);
}
@Override /* ์ค๋ฅ ๋ฐ์์ ํธ์ถ๋จ */
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
System.out.println("Error:" + exception);
super.handleTransportError(session, exception);
}
|
cs |
chat.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>์น์์ผ ํ
์คํธ ํ์ด์ง</title>
<script type="text/javascript">
var userid = '[[${userid}]]';
alert('userid=' + userid);
var g_webSocket = null;
window.onload = function() {
host = "192.168.0.94"; /* ๋ฐฐํฌ์์ ํธ์คํธ ์ฃผ์๋ก ๋ณ๊ฒฝ */
//host = "localhost";
g_webSocket = new WebSocket("ws://"+host+"/ws/chat");
/* ์น์์ผ ์ ์ ์ฑ๊ณต์ ์คํ */
g_webSocket.onopen = function(message) {
addLineToChatBox("Server is connected.");
};
/* ์น์์ผ ์๋ฒ๋ก๋ถํฐ ๋ฉ์์ง ์์ ์ ์คํ */
g_webSocket.onmessage = function(message) {
addLineToChatBox(message.data);
};
/* ์น์์ผ ์ด์ฉ์๊ฐ ์ฐ๊ฒฐ์ ํด์ ํ๋ ๊ฒฝ์ฐ ์คํ */
g_webSocket.onclose = function(message) {
addLineToChatBox("Server is disconnected.");
};
/* ์น์์ผ ์๋ฌ ๋ฐ์์ ์คํ */
g_webSocket.onerror = function(message) {
addLineToChatBox("Error!");
};
}
/* ์ฑํ
๋ฉ์์ง๋ฅผ ํ๋ฉด์ ํ์ */
function addLineToChatBox(_line) {
if (_line == null) {
_line = "";
}
var chatBoxArea = document.getElementById("chatBoxArea");
chatBoxArea.value += _line + "\n";
chatBoxArea.scrollTop = chatBoxArea.scrollHeight;
}
/* Send ๋ฒํผ ํด๋ฆญํ๋ฉด ์๋ฒ๋ก ๋ฉ์์ง ์ ์ก */
function sendButton_onclick() {
var inputMsgBox = document.getElementById("inputMsgBox"); // $('#inputMsgBox').val();
if (inputMsgBox == null || inputMsgBox.value == null || inputMsgBox.value.length == 0) {
return false;
}
var chatBoxArea = document.getElementById("chatBoxArea");
if (g_webSocket == null || g_webSocket.readyState == 3) {
chatBoxArea.value += "Server is disconnected.\n";
return false;
}
// ์๋ฒ๋ก ๋ฉ์์ง ์ ์ก
g_webSocket.send('['+ userid +'] ' + inputMsgBox.value);
inputMsgBox.value = "";
inputMsgBox.focus();
return true;
}
/* Disconnect ๋ฒํผ ํด๋ฆญํ๋ ๊ฒฝ์ฐ ํธ์ถ */
function disconnectButton_onclick() {
if (g_webSocket != null) {
g_webSocket.close();
}
}
/* inputMsgBox ํค ์
๋ ฅํ๋ ๊ฒฝ์ฐ ํธ์ถ */
function inputMsgBox_onkeypress() {
if (event == null) {
return false;
}
// ์ํฐํค ๋๋ฅผ ๊ฒฝ์ฐ ์๋ฒ๋ก ๋ฉ์์ง ์ ์ก
var keyCode = event.keyCode || event.which;
if (keyCode == 13) {
sendButton_onclick();
}
}
</script>
</head>
<body>
<input id="inputMsgBox" style="width: 250px;" type="text" onkeypress="inputMsgBox_onkeypress()">
<input id="sendButton" value="Send" type="button" onclick="sendButton_onclick()">
<input id="disconnectButton" value="Disconnect" type="button" onclick="disconnectButton_onclick()">
<br/>
<textarea id="chatBoxArea" style="width: 100%;" rows="10" cols="50" readonly="readonly"></textarea>
</body>
</html>
|
cs |
โโํน์ ์ธ๋ฌผ๋ง ์ง์ ํ์ฌ ํด๋น ์๋๋ฐฉ์๊ฒ๋ง ๋ฉ์ธ์ง ๋ณด๋ด๊ธฐโโ
WebSocketHandler.java , WSChatController.java , WebSocketConfiguration.java ์์ ์ฝ๋์ ๊ฐ๋ค.
WebSocketHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
import java.util.*;
import java.util.Map.Entry;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import jakarta.servlet.http.HttpSession;
import jakarta.websocket.OnOpen;
import jakarta.websocket.Session;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class WebSocketHandler extends TextWebSocketHandler {
//private final List<WebSocketSession> webSocketSessions = new ArrayList<>();
private static Map<String, WebSocketSession> userMap = new HashMap<>();
@Override /* ํด๋ผ์ด์ธํธ ์ ์์์ ํธ์ถ๋จ */
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
//webSocketSessions.add(session);
System.out.println("Client Connected");
// ์ธํฐ์
ํฐ์ ์ํด HttpSession์ ์์ฑ์ ์๋์ฒ๋ผ WebSocketSession๊ฐ์ฒด๋ก ์ ๋ฌ๋์ด ์ฌ๊ธฐ์ ๋ค๋ฃฐ ์ ์๋ค
HttpSession httpSession = (HttpSession) session.getAttributes().get("httpSession");
log.info("์น์์ผํธ๋ค๋ฌ, httpSession={}", httpSession.getId()); // ์ปจํธ๋กค๋ฌ์ ์ธ์
id์ ๋์ผํ์ง ํ์ธ
String userid = (String) httpSession.getAttribute("userid");
log.info("์น์์ผํธ๋ค๋ฌ, userid={}", userid);
userMap.put(userid, session);
}
@Override /* ์๋ฒ์ ๋ฉ์์ง ๋์ฐฉ์ ํธ์ถ๋จ */
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
System.out.println("Text Message:" + message.getPayload());
JSONParser parser = new JSONParser(); // JSON-Simple ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ
JSONObject jsObj = (JSONObject) parser.parse(message.getPayload().toString());
String sender = (String) jsObj.get("sender");
String receiver = (String) jsObj.get("receiver");
WebSocketSession sess = userMap.get(receiver);
/*for (WebSocketSession webSocketSession : webSocketSessions) {
webSocketSession.sendMessage(message);
}*/
sess.sendMessage(message);
}
@Override /* ์ ์ ํด์ ์ ํธ์ถ๋จ */
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
System.out.println("Connection Closed");
String findUserid = "";
for (Entry<String, WebSocketSession> entry : userMap.entrySet()) {
if (entry.getValue()==session) { // ํค๊ฐ null์ด๋ฉด NullPointerException ์์ธ ๋ฐ์
findUserid = entry.getKey();
userMap.remove(findUserid);
break;
}
}
}
@Override /* ์ค๋ฅ ๋ฐ์์ ํธ์ถ๋จ */
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
System.out.println("Error:" + exception);
super.handleTransportError(session, exception);
}
}
|
cs |
chat.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>์น์์ผ ํ
์คํธ ํ์ด์ง</title>
<script type="text/javascript">
var userid = '[[${userid}]]';
alert('userid=' + userid);
var g_webSocket = null;
window.onload = function() {
host = "localhost"; /* ๋ฐฐํฌ์์ ํธ์คํธ ์ฃผ์๋ก ๋ณ๊ฒฝ */
//host = "localhost 192.168.0.94";
g_webSocket = new WebSocket("ws://"+host+"/ws/chat");
/* ์น์์ผ ์ ์ ์ฑ๊ณต์ ์คํ */
g_webSocket.onopen = function(message) {
addLineToChatBox("Server is connected.");
};
/* ์น์์ผ ์๋ฒ๋ก๋ถํฐ ๋ฉ์์ง ์์ ์ ์คํ */
g_webSocket.onmessage = function(message) {
var jsObj = JSON.parse(message.data);
addLineToChatBox('[' + jsObj.sender +'] ' + jsObj.msg);
};
/* ์น์์ผ ์ด์ฉ์๊ฐ ์ฐ๊ฒฐ์ ํด์ ํ๋ ๊ฒฝ์ฐ ์คํ */
g_webSocket.onclose = function(message) {
addLineToChatBox("Server is disconnected.");
};
/* ์น์์ผ ์๋ฌ ๋ฐ์์ ์คํ */
g_webSocket.onerror = function(message) {
addLineToChatBox("Error!");
};
}
/* ์ฑํ
๋ฉ์์ง๋ฅผ ํ๋ฉด์ ํ์ */
function addLineToChatBox(_line) {
if (_line == null) {
_line = "";
}
var chatBoxArea = document.getElementById("chatBoxArea");
chatBoxArea.value += _line + "\n";
chatBoxArea.scrollTop = chatBoxArea.scrollHeight;
}
/* Send ๋ฒํผ ํด๋ฆญํ๋ฉด ์๋ฒ๋ก ๋ฉ์์ง ์ ์ก */
function sendButton_onclick() {
var inputMsgBox = document.getElementById("inputMsgBox"); // $('#inputMsgBox').val();
if (inputMsgBox == null || inputMsgBox.value == null || inputMsgBox.value.length == 0) {
return false;
}
var chatBoxArea = document.getElementById("chatBoxArea");
if (g_webSocket == null || g_webSocket.readyState == 3) {
chatBoxArea.value += "Server is disconnected.\n";
return false;
}
// ์๋ฒ๋ก ๋ฉ์์ง ์ ์ก
//g_webSocket.send('['+ userid +'] ' + inputMsgBox.value);
//inputMsgBox.value = "";
// inputMsgBox.focus();
var jsObj = {};
jsObj.sender = userid;
jsObj.receiver = 'jone';
jsObj.msg = inputMsgBox.value;
var jsStr = JSON.stringify(jsObj);
g_webSocket.send(jsStr);
inputMsgBox.value = "";
inputMsgBox.focus();
return true;
}
/* Disconnect ๋ฒํผ ํด๋ฆญํ๋ ๊ฒฝ์ฐ ํธ์ถ */
function disconnectButton_onclick() {
if (g_webSocket != null) {
g_webSocket.close();
}
}
/* inputMsgBox ํค ์
๋ ฅํ๋ ๊ฒฝ์ฐ ํธ์ถ */
function inputMsgBox_onkeypress() {
if (event == null) {
return false;
}
// ์ํฐํค ๋๋ฅผ ๊ฒฝ์ฐ ์๋ฒ๋ก ๋ฉ์์ง ์ ์ก
var keyCode = event.keyCode || event.which;
if (keyCode == 13) {
sendButton_onclick();
}
}
</script>
</head>
<body>
<input id="inputMsgBox" style="width: 250px;" type="text" onkeypress="inputMsgBox_onkeypress()">
<input id="sendButton" value="Send" type="button" onclick="sendButton_onclick()">
<input id="disconnectButton" value="Disconnect" type="button" onclick="disconnectButton_onclick()">
<br/>
<textarea id="chatBoxArea" style="width: 100%;" rows="10" cols="50" readonly="readonly"></textarea>
</body>
</html>
|
cs |
์คํ๊ฒฐ๊ณผ :


์์ ์๋ก ์ง์ ํด๋ jone ๋ง ๋ฉ์ธ์ง๋ฅผ ํ์ธํ ์ ์๋ค.

๋ชจ๋ ์ฑํ
์ ์์ ๋ฆฌ์คํธ ๊ตฌํ๊ธฐ
- ์ฑํ
์ ์ ์ํ๋ ๋ชจ๋ ์ด์ฉ์์ ๋ฆฌ์คํธ ์์ฑ
- ํ ์ด์ฉ์๊ฐ ๋ชจ๋ ์ด์ฉ์์ ์์ด๋๋ฅผ ๊ตฌํ๋ ค๋ฉด...
- ๋๊ตฌ๋ ์ ๊ทผํ ์ ์๋ ์์ญ์ ์ ์์ ๋ชฉ๋ก์ ์ ์ง
- ์ ์์๊ฐ ์ด์ฉ์ ๋ชฉ๋ก์ ์์ฒญํ ๋ ์ปจํธ๋กค๋ฌ์์ ๊ทธ ์์ญ์ ์ ์
- ์ ์์ ๋ชฉ๋ก์ JSON ๋ฐฐ์ด ํ์์ผ๋ก ์๋ต
- application ์์ญ(Scope)์ ๋ชจ๋ ์ ์์์ ID๋ฅผ ๊ธฐ๋กํ๋ค
- ServletContext ctx = request.getServletContext();
- ctx.setAttribute("key", "value");
- ctx.setAttribute("allids", "value"); // list, set, map
Set<String> set = new HashSet<>();
ctx.setAttribute("allids", set);
set = ctx.getAttribute("allids");
set.add("์ ์์ ID");
@GetMapping("/allids")
@ResponseBody
public Map<String, List<String>> getAllIds(HttpServletRequest request)
{
Set<String> set = (Set<String>)request.getServletContext().getAttribute("allids");
// set์ผ๋ก๋ถํฐ ์์ด๋๋ฅผ ๊บผ๋ด์ ๋ฆฌ์คํธ์ ๋ด๋๋ค
// ๋ฆฌ์คํธ๋ฅผ ๋งต์ ์ ์ฅ
// ๋งต์ ๋ฆฌํด
}
์น์์ผ ์ ์์
- ServletContext ์ userid ์ ์ฅ
- ์น์์ผ ํธ๋ค๋ฌ์์๋ ServletContext์ ์ ๊ทผ์ด ๊ฐ๋ฅํด์ผ ํจ
- ์ธํฐ์
ํฐ๋ฅผ ํตํด์ ServletContext๋ฅผ ๊ตฌํด์ attributes.put("ctx", ctx ์ฐธ์กฐ๋ณ์);
์น์์ผ ํธ๋ค๋ฌ์์...
ServletContext ctx = (ServletContext)attributes.get.....get("ctx");
๋ชจ๋ ์ด์ฉ์์๊ฒ ์ ์์ ๋ชฉ๋ก ๋ณด๋ด๊ธฐ
WebSocketHandler, afterConnectionEstablished ๋ฉ์๋์ ์๋์ ๋ด์ฉ ์ถ๊ฐ
/* ๋ชจ๋ ์ด์ฉ์์๊ฒ ์ด์ฉ์ ๋ชฉ๋ก์ JSON๋ฌธ์์ด๋ก ์ ์กํ๋ค */
Set<String> set = userMap.keySet();
JSONArray jsArr = new JSONArray();
Iterator<String> it = set.iterator();
while(it.hasNext())
{
String uid = it.next();
jsArr.add(uid);
}
JSONObject jsObj = new JSONObject();
jsObj.put("allids", jsArr);
String jsStr = jsObj.toJSONString();
session.sendMessage(new TextMessage(jsStr));
๋๊ธ