티스토리 뷰

+메이븐 설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-websocket</artifactId>
    <version>${org.springframework-version}</version>
</dependency>
        
<dependency
    <groupId>org.springframework</groupId>
    <artifactId>spring-messaging</artifactId
    <version>${org.springframework-version}</version
</dependency>
                
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.2.3</version>
</dependency>
cs



+웹소켓 요청에 대한 핸들러 작성

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
@Component
public class ContentHandler extends TextWebSocketHandler {
    
    List<WebSocketSession> userList;
    
    public ContentHandler(){
        this.userList = new LinkedList<WebSocketSession>();
    }
 
    
    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
 
        String payloadMessage = (String) message.getPayload();
        try {
            
            ResultContent rc = new ResultContent();
            
            for(WebSocketSession socketSession : userList){
                socketSession.sendMessage(new TextMessage(payloadMessage));
            }
            
        } catch (JsonParseException jpe) {
            jpe.printStackTrace();
        } catch (JsonMappingException jme) {
            jme.printStackTrace();
        }
    }
    
    
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        session.getRemoteAddress().getHostName();
        userList.add(session); //접속 클라이언트의 세션 저장
    }
    
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        userList.remove(session); //접속 클라이언트의 세션 삭제
    }
    
    
}
cs



+웹소켓 설정으로 작성한 핸들러 등록

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Configuration
@EnableWebMvc
@EnableWebSocket
public class WebsocketConfig implements WebSocketConfigurer {
 
    @Resource(name="contentHandler")
    ContentHandler handler;
    
    
    
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(handler, "/contentPush").withSockJS();
    }   
    
    public void configureDefaultServletHandling(
    DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
 
}
cs




+ 자바스크립트 소켓 객체 생성

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
 if ("WebSocket" in window)
      {
        var ws = new SockJS("http://${urlInfo}/contentPush");
         
         
         ws.onopen = function()
         {
             //웹 소켓이 연결되었을 때
            ws.send('안녕하세요.');
         };
            
         
          ws.onmessage = function (evt) 
         { 
            //웹 소켓 메세지를 보냈을 때 
            var received_msg = evt.data;
            
            alert(received_msg);
            
            
         };
            
         ws.onclose = function()
         { 
             //웹 소켓이 닫힐 때
         //   alert("Connection is closed..."); 
         }; 
      }
      
      else
      {
         // 여기는 웹소켓이 지원되지 않는 브라우저일 때
      }
cs


'SPRING > 정리' 카테고리의 다른 글

@RequestMapping 핸들러 매핑  (0) 2016.08.24
스프링의 컨트롤러, 핸들러 매핑, 인터셉터  (4) 2016.08.23
빈 초기화 메서드 지정  (0) 2016.08.21
스코프(scope) 빈  (0) 2016.08.20
프로토타입(prototype) 빈  (0) 2016.08.19
Comments
최근에 올라온 글
최근에 달린 댓글
TAG
more
Total
Today
Yesterday