Skip to content

Commit

Permalink
feat: set WebsocketConfig and front-test-bed
Browse files Browse the repository at this point in the history
  • Loading branch information
hyun98 committed Oct 14, 2023
1 parent aa765bc commit 127403b
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 1 deletion.
13 changes: 13 additions & 0 deletions front-test-bed/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Websocket Simple Test

1. ## Click `Edit Configurations...`
![Edit-Configurations](editConfig.png)

2. ## Input `dev` in 'Active profiles'
![Alt text](setProfile.png)

> dev 프로필로 실행하면 h2 db를 사용해 MySQL의 실행 없이 간단한 테스트가 가능합니다.
3. ## Run!

4. ## Open `front-test-bed/index.html`
60 changes: 60 additions & 0 deletions front-test-bed/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const stompClient = new StompJs.Client({
brokerURL: 'ws://localhost:8080/ws-stomp'
});

stompClient.onConnect = (frame) => {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/sub/greetings', (greeting) => {
showGreeting(JSON.parse(greeting.body).content);
});
};

stompClient.onWebSocketError = (error) => {
console.error('Error with websocket', error);
};

stompClient.onStompError = (frame) => {
console.error('Broker reported error: ' + frame.headers['message']);
console.error('Additional details: ' + frame.body);
};

function setConnected(connected) {
$("#connect").prop("disabled", connected);
$("#disconnect").prop("disabled", !connected);
if (connected) {
$("#conversation").show();
}
else {
$("#conversation").hide();
}
$("#greetings").html("");
}

function connect() {
stompClient.activate();
}

function disconnect() {
stompClient.deactivate();
setConnected(false);
console.log("Disconnected");
}

function sendName() {
stompClient.publish({
destination: "/pub/hello",
body: JSON.stringify({'name': $("#name").val()})
});
}

function showGreeting(message) {
$("#greetings").append("<tr><td>" + message + "</td></tr>");
}

$(function () {
$("form").on('submit', (e) => e.preventDefault());
$( "#connect" ).click(() => connect());
$( "#disconnect" ).click(() => disconnect());
$( "#send" ).click(() => sendName());
});
Binary file added front-test-bed/editConfig.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions front-test-bed/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!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="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@stomp/[email protected]/bundles/stomp.umd.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>
Binary file added front-test-bed/setProfile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package joryu.sns_service.chat.config

import org.springframework.context.annotation.Configuration
import org.springframework.messaging.simp.config.MessageBrokerRegistry
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
class WebSocketConfig : WebSocketMessageBrokerConfigurer {

override fun configureMessageBroker(config: MessageBrokerRegistry) {
config.enableSimpleBroker("/sub")
config.setApplicationDestinationPrefixes("/pub")
}

override fun registerStompEndpoints(registry: StompEndpointRegistry) {
registry
.addEndpoint("/ws-stomp")
.setAllowedOriginPatterns("*")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package joryu.sns_service.chat.controller

import joryu.sns_service.chat.message.GreetingMessage
import joryu.sns_service.chat.message.HelloMessage
import org.springframework.messaging.handler.annotation.MessageMapping
import org.springframework.messaging.handler.annotation.SendTo
import org.springframework.stereotype.Controller

@Controller
class GreetingController {

@MessageMapping("/hello")
@SendTo("/sub/greetings")
fun greeting(message: HelloMessage): GreetingMessage {
Thread.sleep(100)
return GreetingMessage("Hello, " + message.name + "!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package joryu.sns_service.chat.message

data class GreetingMessage (
val content: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package joryu.sns_service.chat.message

data class HelloMessage (
val name: String
)
29 changes: 28 additions & 1 deletion sns_service/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ spring:
password: 1234
url: jdbc:mysql://localhost:33061/sns-db?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&useLegacyDatetimeCode=false
driver-class-name: com.mysql.cj.jdbc.Driver

jpa:
hibernate:
ddl-auto: create
Expand All @@ -13,9 +12,37 @@ spring:
show_sql: true
format_sql: true
dialect: org.hibernate.dialect.MySQL8Dialect
profiles:
group:
dev: "local"
active: local

management:
endpoints:
web:
exposure:
include: "prometheus"

---

spring:
config:
activate:
on-profile: "local"
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:testdb;MODE=MySQL;
username: sa
password:
jpa:
hibernate:
ddl-auto: create-drop
logging.level:
org.hibernate.SQL: debug
properties:
hibernate:
show_sql: true
data:
redis:
host: localhost
port: 6379

0 comments on commit 127403b

Please sign in to comment.