기본 콘텐츠로 건너뛰기

WebSocket

WebSocket을 사용한 채팅 프로그램 만들기
- Spring boot 기반 고수준
- 웹사이트 접속자 전체를 대상으로 한 채팅
- 특정 이용자 그룹/개인을 대상으로 한 채팅
- http 프로토콜은 접속요청 후 접속되면 응답을 전송하고 접속 해제
- 웹서버 상에 서버소켓을 두고 웹브라우저가 서버소켓에 접속요청
ServerSocket : 네트워크(TCP/IP) 서버, 무한히 클라이언트를 대기
Socket : 서버에 접속요청, 통신가능

특정 이용자간의 통신(채팅)
로그인한 후 이용자의 아이디 활용(HttpSession)
WebSocket 클래스에서 메시지 송수신 부분에서
특정 아이디를 가진 이용자의 소켓에 메시지를 전송해야 한다
메세지 구성(JSON)
- 송신자, 수신자, 컨텐츠
서버측에서는 메시지를 가로채서 수신자가 누군지 확인/해당 이용자에게만 전송
서버측에서 JSON을 다루기 위해서 json-simple 사용(dependency 추가)

모든 채팅 접속자 리스트 구하기
- 채팅에 접속하는 모든 이용자의 리스트 작성
- 한 이용자가 모든 이용자의 아이디를 구하려면...
- 누구나 접근할 수 있는 영역에 접속자 목록을 유지
- 접속자가 이용자 목록을 요청할 때 컨트롤러에서 그 영역에 접속
- 접속자 목록을 JSON 배열 형식으로 응답
- application 영역(Scope)에 모든 접속자의 ID를 기록한다
- ServletContext sc = request.getServletContext();
- sc.setAttribute("userSet",userSet); // list, set, map

Controller / WebSocket
- Configurator : WebSocket에서 HttpSession을 사용할 수 있도록 설정

pom.xml
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-websocket -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>3.0.1</version>
</dependency>

이 블로그의 인기 게시물

Python Sklearn make_regression

from sklearn.datasets import make_regression import matplotlib.pyplot as plt X, y = make_regression(n_samples=250, n_features=1, noise=50, random_state=2) plt.scatter(X,y, s=2) plt.show() from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split # 한글 깨짐 없이 나오게 설정 from matplotlib import rcParams # 인코딩 폰트 설정 rcParams['font.family'] = 'New Gulim' rcParams['font.size'] = 10 x_train, x_test, y_train, y_test = train_test_split(X,y, test_size=.20, random_state=0) x_train.shape, x_test.shape, y_train.shape, y_test.shape # 모델 생성 model = LinearRegression() # 학습하기 model.fit(x_train, y_train) # 가중치, 편향치 구하기 model.coef_, model.intercept_ # (array([90.11061494]), 2.4224269924448585) # 결정 계수 model.score(x_train, y_train) # 0.789267454050733 # 추정 pred = model.predict(x_test) # 산점도 plt.scatter(x_test,y_test) plt.plot(x_test, pred, 'r-') plt.show() # 추정 model.predict([[3.0]]) # 학습할 때 주는 데이터의 형식을 따른다 # x의 최소값, 최대값을 계수와 절편을 사용하여 ...

Javascript on 함수

엔터키 감지하기 <input type="password" onkeypress="func(event)" /> function func(event) {      if(event.keyCode == 13) { // keyCode 13은 엔터이다           alert("엔터를 입력했습니다.");     }     if (event.tartget.value == 13) {          alert("엔터를 입력했습니다.");     } }

Grid 정렬

  .container { display : grid ; gap : 22px ; width : 1000px ; grid-template-columns : repeat ( auto-fit , 150px ); margin : auto ; justify-content : center ; } .container {      display : grid ; gap : 22px ; grid-template-columns : repeat ( auto-fit , minmax ( 250px , 1fr )); }