[spring boot]_21일차, 22일차_채팅기능 구현_WebSocket

youuu·2022년 11월 15일
0

SPRING

목록 보기
29/33

meta파일이 꼬였을때 팁.
sts 끈후 .meta파일 날린후 import로 springsrc 를 한다.


🌱🌱 채팅


WebSocket 을 사용하여 만든다.
WebSocket 참고예제 를 보고 만들어보기.
-> 수업시간엔 선생님이 문제생겨서 못진행했다.

1. Project dependencies 생성시 web Socket 설정

2. 부트 프로젝트 설정하기(pom.xml)

<!--웹 소켓 사용 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<!-- json simple json파싱을 위해
json-simple 라이브러리를 추가 -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>

3. WebSocketConfig 설정

1) 지정 Package

  • com.oracle.bootMyBatis03.configuration
    2) 웹소켓 구현체와 등록해주는 config파일을 생성, 관련 class
// 환경설정 annotation
@Configuration 
@EnableWebSocket 
public class WebSocketConfig implements WebSocketConfigurer {
 @Autowired
 SocketHandler socketHandler; // SocketHandler DI
 @Override
 public void registerWebSocketHandlers
 (WebSocketHandlerRegistry registry) {
 registry.addHandler(socketHandler, "/chating");
 }
}

4. 구현체에 등록할 SocketHandler.java

  1. afterConnectionEstablished 메소드는 웹소켓 연결되면 동작.
  2. afterConnectionClosed 메소드는 반대로 웹소켓이 종료되면 동작
  3. handleTextMessage 메소드는 메시지를 수신하면 실행.
    상속받은 TextWebSocketHandler는 handleTextMessage를 실행시키며, 메시지 타입에따라 handleBinaryMessage 또는 handleTextMessage가 실행
  4. 웹소켓 세션을 담아둘 맵
    HashMap<String, WebSocketSession>
    sessionMap = new HashMap<>();

@SuppressWarnings

@SuppressWarning
컴파일 단위의 서브세트와 관련된 컴파일 경고를 사용하지 않도록 설정하는 것.

따라서 어떤 경고를 제외시킬지 옵션
1. all : 모든 경고를 억제
2. cast : 캐스트 연산자 관련 경고 억제
3. dep-ann : 사용하지 말아야 할 주석 관련 경고 억제
4. deprecation : 사용하지 말아야 할 메소드 관련 경고 억제
5. fallthrough : switch문에서의 break 누락 관련 경고 억제
6. finally : 반환하지 않는 finally 블럭 관련 경고 억제
7. null : null 분석 관련 경고 억제
8. rawtypes : 제네릭을 사용하는 클래스 매개 변수가 불특정일 때의 경고 억제
9. unchecked : 검증되지 않은 연산자 관련 경고 억제
10. unused : 사용하지 않는 코드 관련 경고 억제
참고자료


chatView.jsp

  1. 이름 유효성 체크
    -> 없으면 alert("사용자 이름을 입력해주세요.");
    -> 있으면

핸들러가 서버


profile
공부중인 주니어 개발자

0개의 댓글