-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
임시 임시 임시 임시
- Loading branch information
Showing
7 changed files
with
176 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
const accessToken = "Bearer " + getCookie('access'); // header or cookie 사용 | ||
|
||
// 소켓 연결을 위한 객체 생성 | ||
const stompClient = new StompJs.Client({ | ||
brokerURL: 'wss://ssalon.co.kr/ws-stomp', | ||
connectHeaders: { | ||
Authorization: accessToken, | ||
}, | ||
}); | ||
|
||
// 소켓 연결 시도 | ||
function connect() { | ||
stompClient.activate(); | ||
} | ||
|
||
// 소켓 연결이 된 직후 채팅방 구독(subscribe) | ||
stompClient.onConnect = (frame) => { | ||
setConnected(true); | ||
console.log('Connected: ' + frame); | ||
stompClient.subscribe('/room/1', (greeting) => { // 특정 모임 아이디로 설정 | ||
showMessage(JSON.parse(greeting.body)); | ||
}); | ||
stompClient.publish({ | ||
destination: "/send/1", // 특정 모임 아이디로 설정 | ||
headers: { | ||
"Authorization": accessToken, | ||
"MessageType": "ENTER", | ||
}, | ||
body: JSON.stringify({ | ||
'message': "입장하였습니다.", | ||
}) | ||
}); | ||
}; | ||
|
||
// 메시지 전송(publish) | ||
function sendMessage() { | ||
stompClient.publish({ | ||
destination: "/send/1", // 특정 모임 아이디로 설정 | ||
headers: { | ||
"Authorization": accessToken, | ||
"MessageType": "TALK", | ||
}, | ||
body: JSON.stringify({ | ||
'message': $("#message").val(), | ||
}) | ||
}); | ||
} | ||
|
||
// 메시지를 html에 출력 | ||
function showMessage(messageObj) { // messageObj = nickname, message, profilePicture, date | ||
$("#greetings").append("<tr><td>" + messageObj.nickname + " : " + messageObj.message + "</td></tr>"); | ||
} | ||
|
||
// 기존 채팅 불러오기 | ||
function loadChat(chatList) { | ||
// GET /api/chat/{moimId} API 호출 | ||
} | ||
|
||
// 소켓 연결 여부에 따른 UI 변경 | ||
function setConnected(connected) { | ||
$("#connect").prop("disabled", connected); | ||
$("#disconnect").prop("disabled", !connected); | ||
if (connected) { | ||
$("#conversation").show(); | ||
} | ||
else { | ||
$("#conversation").hide(); | ||
} | ||
$("#greetings").html(""); | ||
} | ||
|
||
// 소켓 연결 해제 | ||
function disconnect() { | ||
stompClient.deactivate(); | ||
setConnected(false); | ||
console.log("Disconnected"); | ||
} | ||
|
||
// 에러 핸들링 | ||
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 () { | ||
$("form").on('submit', (e) => e.preventDefault()); | ||
$( "#connect" ).click(() => connect()); | ||
$( "#disconnect" ).click(() => disconnect()); | ||
$( "#send" ).click(() => sendMessage()); | ||
}); | ||
|
||
// 쿠키 가져오기 | ||
function getCookie(name) { | ||
let matches = document.cookie.match(new RegExp( | ||
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)" | ||
)); | ||
return matches ? decodeURIComponent(matches[1]) : undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<!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="message">message?</label> | ||
<input type="text" id="message" class="form-control" placeholder="Your message 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> |