๋ฉ์์ด ์ฌ์์ฒ๋ผ - SWU์์ ์งํํ๋ ํ๋ก์ ํธ์ธ 'UniLearn(์ ๋๋ฐ)'์ด ๊ฐ๋ฐ ์ฐฉ์ํฉ๋๋ค.
11์ 6์ผ์ ๋น๋๋ฉด์ผ๋ก ํ๋ก์ ํธ ๋ณด๊ณ ํํ๊ธฐ ์ ๊น์ง
ํดํ์ ํ์ ํ ๋ถ์ด
jwt ๋ก ์ธ์ฆ,์ธ๊ฐ๊ตฌํ์ ํตํด ํ์๊ฐ์ ๊ณผ ๋ก๊ทธ์ธ ๋ก์ง์ ์ง์ฃผ์ ์
์ถ๊ฐ์ ์ผ๋ก ์ฑํ ์ ๊ตฌํํ ์์์ ์ง๋ง ๊ณต๋ถํด์ค๊ธฐ๋ก ํ์ต๋๋ค.
๋จ์ ๊ฐ๋ฐ์ ๊ฐ์งํค์ด ๋๋๊ณ ์งํํ๊ธฐ๋ก ํ์ต๋๋ค.
์ฑํ ์ ์์ ์๋ฌด๊ฒ๋ ๋ชจ๋ฅด๋ ์ ๋ ์์ ๋๊ฐ์
๋ธ๋ก๊ทธ ํ๋๋ฅผ ์ฐธ๊ณ ํ์ต๋๋ค. ์ถ๊ฐ์ ์ผ๋ก spring ์ฌ์ดํธ๋ ์ฐธ๊ณ ํ๋ค์
https://www.youtube.com/watch?v=8iwNDEXldAk
https://www.youtube.com/watch?v=rvss-_t6gzg
๋ธ๋ก๊ทธ :
https://growth-coder.tistory.com/157
https://spring.io/guides/gs/messaging-stomp-websocket/
์น ์์ผ์ html5๊ฐ ๋์ค๋ฉด์ ์๊ฐ๋์์ผ๋ฉฐ
Ajax ํต์ ์ผ๋ก ํ ์๋ ์๋๋ฐ ์์ผํต์ ์ผ๋ก ํ๋ฉด ์ข์ ์ ์,
์ฐ๊ฒฐ ์ค์ ์๋ค๊ฐ๋ค ํ๋ ๋ฐ์ดํฐ์ ํค๋๊ฐ ํฌํจ์ด ์๋๊ธฐ ๋๋ฌธ์ ์ฉ๋์ด ์๋ค๋ ์ด์ ์ด ์๋ค๋ ๊ฒ์ ๋๋ค.
์คํ๋ง - ๊ฐ์ด๋ - websocket์ ์ฐธ๊ณ ํ๋ฉด
https://spring.io/guides/gs/messaging-stomp-websocket/
Getting Started | Using WebSocket to build an interactive web application
In Spring’s approach to working with STOMP messaging, STOMP messages can be routed to @Controller classes. For example, the GreetingController (from src/main/java/com/example/messagingstompwebsocket/GreetingController.java) is mapped to handle messages t
spring.io
์ฌ๊ธฐ์ ํน์ง์ ์ผ๋ก ๋์ค๋ ๋จ์ด๊ฐ ์๋๋ฐ,
STOMP ์ ๋๋ค.
์ฐ๋ฆฌ๋ stomp๋ผ๋ ์คํ์ ์ฌ์ฉํด์ ๊ตฌํํด๋ณผ ๊ฒ์ ๋๋ค. ๊ทผ๋ฐ ๊ตณ์ด stomp ์ฌ์ฉํ์ง ์์๋ ์น์์ผ์ ์ฌ์ฉ๊ฐ๋ฅํฉ๋๋ค
STOMP : Simple (or Streaming) Text Orientated Messaging Protocol.
ํ๋กํ ์ฝ์ด๋, ๊ท์ฝ์ด๋ผ๊ณ ์๊ฐํ์๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค.
์คํ๋ง์์๋ STOMP๋ฅผ ์ฌ์ฉํด์ ์น ์์ผ์ ๊ตฌํํ ์ ์์ต๋๋ค.
๊ฐ๋จํ ๋ฉ์์ง๋ฅผ ์ ์กํ๊ธฐ ์ํ ํ๋กํ ์ฝ๋ก ๋ฉ์์ง ๋ธ๋ก์ปค๋ฅผ์ publisher - subscriber ๋ฐฉ์์ ์ฌ์ฉํ๋๋ฐ์.
๋ฉ์์ง์ ๋ฐํ์์ ๊ตฌ๋ ์๊ฐ ์กด์ฌํ๊ณ ๋ฉ์์ง๋ฅผ ๋ณด๋ด๋ ์ฌ๋๊ณผ ๋ฐ๋ ์ฌ๋์ด ๊ตฌ๋ถ๋์ด ์์ผ๋ฉฐ
๋ฉ์์ง ๋ธ๋ก์ปค๋ ๋ฐํ์๊ฐ ๋ณด๋ธ ๋ฉ์์ง๋ฅผ ๊ตฌ๋ ์์๊ฒ ์ ๋ฌํด์ฃผ๋ ์ญํ ์ ํฉ๋๋ค.
STOMP๋ HTTP์ ๋น์ทํ๊ฒ frame ๊ธฐ๋ฐ ํ๋กํ ์ฝ command, header, body๋ก ์ด๋ฃจ์ด์ ธ ์์ด์
๊ฐ๋ฐ์ ๋๊ตฌ๋ก ์ง์ ํ์ธ๋ ๊ฐ๋ฅํด์
<STOMP frame ๊ตฌ์กฐ>
COMMAND
header1:value1
header2:value2
Body^@
์คํ๋ง ์น ์์ผ ๊ตฌํ
https://spring.io/guides/gs/messaging-stomp-websocket/
์คํ๋ง - ๊ฐ์ด๋ - websocket
Getting Started | Using WebSocket to build an interactive web application
In Spring’s approach to working with STOMP messaging, STOMP messages can be routed to @Controller classes. For example, the GreetingController (from src/main/java/com/example/messagingstompwebsocket/GreetingController.java) is mapped to handle messages t
spring.io
์ฌ๊ธฐ ๋ค ๋์ ์์ต๋๋ค. ๋๋ต์ ์ธ๊ฑด..
start.spring.io์์ websocket dependency๋ฅผ ์ถ๊ฐํด์คฌ์ต๋๋ค.
Spring Boot : 2.7.17
Packaging : Jar
Java : 11
ํ๋ก์ ํธ๊ฐ ๋ง๋ค์ด์ง๋ฉด
์ฌ๋ฌ ํ์ผ์ ์์ฑํด์ค์ผ ํฉ๋๋ค.
<build.gradle>
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.webjars:webjars-locator-core'
implementation 'org.webjars:sockjs-client:1.5.1'
implementation 'org.webjars:stomp-websocket:2.3.4'
implementation 'org.webjars:bootstrap:5.2.3'
implementation 'org.webjars:jquery:3.6.4'
implementation 'org.projectlombok:lombok:1.18.26'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
์๋ก ๋ง๋ <index.html>
<!DOCTYPE html>
<html>
<head>
<title>Hello WebSocket</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="/main.css" rel="stylesheet">
<script src="/webjars/jquery/jquery.min.js"></script>
<script src="/webjars/sockjs-client/sockjs.min.js"></script>
<script src="/webjars/stomp-websocket/stomp.min.js"></script>
<script src="/app.js"></script>
</head>
<body>
<noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websocket relies on Javascript being
enabled. Please enable
Javascript and reload this page!</h2></noscript>
<div id="main-content" class="container">
<div class="row">
<div class="col-md-6">
<form class="form-inline">
<div class="form-group">
<label for="connect">WebSocket connection:</label>
<button id="connect" class="btn btn-default" type="submit">Connect</button>
<button id="disconnect" class="btn btn-default" type="submit" disabled="disabled">Disconnect
</button>
</div>
</form>
</div>
<div class="col-md-6">
<form class="form-inline">
<div class="form-group">
<label for="name">What is your name?</label>
<input type="text" id="name" class="form-control" placeholder="Your name here...">
</div>
<button id="send" class="btn btn-default" type="submit">Send</button>
</form>
</div>
</div>
<div class="row">
<div class="col-md-12">
<table id="conversation" class="table table-striped">
<thead>
<tr>
<th>Greetings</th>
</tr>
</thead>
<tbody id="greetings">
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
<app.js>
var stompClient = null;
function setConnected(connected) {
$("#connect").prop("disabled", connected);
$("#disconnect").prop("disabled", !connected);
if (connected) {
$("#conversation").show();
}
else {
$("#conversation").hide();
}
$("#greetings").html("");
}
function connect() {
var socket = new SockJS('/app/gs-guide-websocket');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/greetings', function (greeting) {
showGreeting(JSON.parse(greeting.body).content);
});
});
}
function disconnect() {
if (stompClient !== null) {
stompClient.disconnect();
}
setConnected(false);
console.log("Disconnected");
}
function sendName() {
stompClient.send("/app/hello", {}, JSON.stringify({'name': $("#name").val()}));
}
function showGreeting(message) {
$("#greetings").append("<tr><td>" + message + "</td></tr>");
}
$(function () {
$("form").on('submit', function (e) {
e.preventDefault();
});
$( "#connect" ).click(function() { connect(); });
$( "#disconnect" ).click(function() { disconnect(); });
$( "#send" ).click(function() { sendName(); });
});
<HelloMessage.java>
package webSocketTest.webSocket;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
public class HelloMessage {
private String name;
public HelloMessage(){
}
public HelloMessage(String name){ this.name = name;}
public String getName(){ return name;}
public void setName(String name) {this.name = name;}
}
<Greeting.java>
package webSocketTest.webSocket;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
public class Greeting {
private String content;
public Greeting(){
}
public Greeting(String content){this.content = content;}
public String getContent(){return content;}
}
<GreetingController>
package webSocketTest.webSocket;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.util.HtmlUtils;
@Controller
public class GreetingController {
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
Thread.sleep(500); // simulated delay
return new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
}
}
<WebSocketConfig>
package webSocketTest.webSocket;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.stereotype.Controller;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
@Configuration
@EnableWebSocketMessageBroker
@Controller
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/app/gs-guide-websocket").withSockJS();
}
}
<WebSocketApplication> ์ด๊ฑฐ๋ ๊ธฐ๋ณธ์ผ๋ก ์๋๊ฑฐ๊ณ ..
์ข๋ค. ์ด์ WebSocketApplication ์ ๋ค์ด๊ฐ์ ์คํ๋ฒํผ ๋๋ฅด๊ณ
๊ฒ์์ฐฝ์ localhost:8080์ ๊ฒ์ํ๋ค.
---
์ฌ๊ธฐ์ ๋ถํฐ ์ข์ถฉ์ฐ๋ ์๋ฌ ๋ฐ์๊ธฐ ....
๋๋ ๊ทธ๋ฅ ํ๋ผ๋ ๋๋ก ํ๊ฑด๋ฐ ์ ์๋ฌ๊ฐ.. ... ..
connet๊ฐ ์๋๋ค. ์.. ?
f12๋ฅผ ๋๋ฌ ๊ฐ๋ฐ์ ๋ชจ๋์ ๋ค์ด๊ฐ์ ์๋ฌ๋ฅผ ํ์ธํ๋ค. connet๊ฐ ์๋๋ ธ๊ฑฐ๋ ..
1. Failed to load resource: the server responded with a status of 404 () stomp.min.js:8 Whoops! Lost connection to http://localhost:8080/gs-guide-websocket stomp.min.js:8 Opening Web Socket... :8080/gs-guide-websocket/info?t=1699014111573:1
์น ์์ผ์ ์ด ๋ gs-guide-websocket์ ๋ชป์ฐพ๊ฒ ๋ค ๋ ๋ป ์ธ๊ฑฐ ๊ฐ๋ค. ๋์ถฉ?
Can´t establish connection to websocket "Whoops! Lost connection to http://localhost:8080"
I´m trying to implement a websocket in my spring boot application, but I´m unable to create a connection. I used this video and its corresponding git-repo to create the following config for the ser...
stackoverflow.com
์คํ ์ค๋ฒํ๋ก์ฐ ์งฑ
app.js์์ connect() ํจ์์ socket path ์์ /app/์ ๋ง๋ถ์ฌ ์ฃผ์๋ค.
์ด์ ๋ค๋ฅธ ์๋ฌ๊ฐ ๋์๋ค.
2. Uncaught ReferenceError: SockJS is not defined ์๋ฌ
https://jindevelopetravel0919.tistory.com/96
[์๋ฌ] Uncaught ReferenceError: SockJS is not defined
[Reason] WebSocket ์ ์ฐ๊ฒฐํ๊ณ ๊ฐ๋จํ๊ฒ ์ ์์ ์ผ๋ก ๋์์ด ์ํ๋๋์ง ํ์ธํ๊ธฐ ์ํด ๊ฐ๋จํ ์ ์ฌ 1์ธ ์ฑํ ํ์ด์ง๋ฅผ ๋ง๋ค๊ณ ์คํํ์ผ๋ "Uncaught ReferenceError: SockJS is not defined" ํด๋น ์๋ฌ๊ฐ ๋ฐ์ํ
jindevelopetravel0919.tistory.com
index.html ์ header ๋ถ๋ถ์ sockjs์ ๊ด๋ จํ ์ฝ๋๋ฅผ ๋ฃ์ด์ฃผ์๋ค.
3. Uncaught ReferenceError: Stomp is not defined ์๋ฌ
์ข์ . ์์ script ๋ค์ ๊ฐ์๋ผ์ ๋ค.
์ด์ ์ฌ์ฌ ๋๋ ค์น๊ณ ์ถ์๋ค..
4.
๊ทผ๋ฐ ์ด๊ฑฐ ๊ตฌ๊ธ๋ง ํด๋ ์๋์ค๊ธธ๋ ,,
์ฒซ๋ฒ์งธ ์๋ฌ๋ ๋น์ทํด์ ๋๋ ค๋ง์ท๋๋ ๋๋ค.
addEndpoint path์ /app/์ ์ถ๊ฐ ํด์ฃผ์๋ค.
----
๊ฒฐ๊ตญ ๋๋ค. ์น์์ผ ์ฑ๊ณต
ํญ์ ํ๋ ๋ ์ด์ด์ ์ ์ํด๋ณด๋ฉด ๋ค๋ฅธ ํญ์์ ๋ณด๋ธ ์ด๋ฆ์ด ๋ณด์ด๋ ๊ฒ๋ ํ์ธํ ์ ์๋ค.
์ด๋ ๊ฒ ํ๋ฉด ์คํ๋ง์ผ๋ก ์ค์๊ฐ ์ฑํ ๊ตฌํํ๋ ๊ฒ๋ ๋ฌธ์ ์๋ค!
์๋ ๊ทธ๊ฑด ๋ค๋ฅด๋ ค๋..
๊ณต๋ถํ๋ฉด์ ๋๋ ์ ์ ..
์์ด ๊ณต๋ถํด์ผ๊ฒ ๋ค. ...
์คํ์ค๋ฒํ๋ก์ฐ ์งฑ..
๐๊นํ๋ธ ๋งํฌ
https://github.com/hansun-hub/webSocket
GitHub - hansun-hub/webSocket: 11-03 ์๋ฉ์ฌ ํ๋ก์ ํธ๋ฅผ ์ํ ์คํ๋ง ์์ผํ๋ก๊ทธ๋๋ฐ์ ๋๋ค. STOMP ์ฌ์ฉ
11-03 ์๋ฉ์ฌ ํ๋ก์ ํธ๋ฅผ ์ํ ์คํ๋ง ์์ผํ๋ก๊ทธ๋๋ฐ์ ๋๋ค. STOMP ์ฌ์ฉ. Contribute to hansun-hub/webSocket development by creating an account on GitHub.
github.com