Lombok 추가
프로젝트구조
build.gradle
스프링부트 버전을 2.6.1 로 해준다.
sourceCompatibility 을 11 로 해준다.
plugins {
id 'java'
id 'war'
id 'org.springframework.boot' version '2.6.1'
id 'io.spring.dependency-management' version '1.1.2'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '11'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.projectlombok:lombok:1.18.16'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
com.example.websocket 패키지 2개 파일 추가
- WebSocket.java
- WebScoektConfig.java
WebSocket.java
package com.example.websocket;
import org.springframework.stereotype.Service;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
@Service
@ServerEndpoint("/webSocket")
public class WebSocket {
@OnMessage
public void onMessage(String msg, Session session) throws Exception{
System.out.println("/webSocket onMessage...........");
}
@OnOpen
public void onOpen(Session s) {
System.out.println("/webSocket open...........");
}
@OnClose
public void onClose(Session s) {
System.out.println("/webSocket onClose...........");
}
}
WebScoektConfig.java
package com.example.websocket;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Component
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
controller 패키지 추가후 testController.java 추가
package com.example.websocket.controller;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/test/")
@RequiredArgsConstructor
public class testController {
@GetMapping("/websocket")
public String websocket(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
System.out.println("웹소켓 테스트 컨트롤러...................................");
// test폴더에 websocket.html화일로 구성
return "test/index";
}
}
resources test 폴더 추가후 index.html 추가
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebSocket</title>
</head>
<script>
const webSocket = new WebSocket("ws://localhost:8080/webSocket");
// 웹 소켓 연결 이벤트
webSocket.onopen = function () {
alert("웹소켓 서버와 연결에 성공했습니다.");
};
// 웹 소켓 메세지 수신
webSocket.onmessage = function (event) {
alert(event.data);
};
// 웹 소켓 연결 종료
webSocket.onclose = function () {
alert("웹소켓 서버와 연결이 종료되었습니다.");
};
// 오류 발생
webSocket.onerror = function (error) {
console.log(error);
};
</script>
<body>
</body>
</html>
서버시작후 웹브라우져 접속
http://localhost:8080/test/websocket
'프로그래밍 > Java' 카테고리의 다른 글
intelliJ Springboot + MyBatis, log4jdbc로 console log 설정(2) (0) | 2023.08.18 |
---|---|
intelliJ Springboot + MyBatis + MySQL 셋팅 및 연동(1) (0) | 2023.08.18 |
intellij java11 + spring boot + 웹소켓 서버 (WebSocket Server) 구축 (0) | 2023.05.03 |
java 스케쥴러 중복 실행될때 (0) | 2022.02.23 |
Java 정규식 + split (0) | 2022.02.10 |