-
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
1 parent
ff85e20
commit 5d797ee
Showing
5 changed files
with
253 additions
and
159 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Socket-IO test</title> | ||
</head> | ||
|
||
<body class="p-5"> | ||
<h1>Chat Test:</h1> | ||
<div class="flex w-full"> | ||
<div class="basis-6/12 w-full"> | ||
<label for="">JWT Token:</label> | ||
<input type="text" name="jwt_token" id="jwt_token" class="w-1/3"> | ||
<br><br> | ||
<label for="">To User:</label> | ||
<input type="text" name="to_user" id="to_user" value="3" class="w-1/3"> | ||
<br><br> | ||
<button onclick="loadChat()">getChat</button> | ||
</div> | ||
<div class="basis-6/12 w-full"> | ||
<div id="chat_box" class="w-full overflow-y-scroll h-[30vw] max-h-[30vw] border-2 bg-gray-100"> | ||
<div id="chat_history" class="w-full"></div> | ||
</div> | ||
<br><br> | ||
<div class="flex w-full"> | ||
<input type="text" name="chat_input" id="chat_input" class="w-full"> | ||
<button onclick="sendChat()">sendChat</button> | ||
</div> | ||
</div> | ||
</div> | ||
</body> | ||
<script src="https://cdn.tailwindcss.com"></script> | ||
<script> | ||
tailwind.config = { theme: { extend: {} } } | ||
</script> | ||
<style type="text/tailwindcss"> /* @layer utilities { | ||
.content-auto { | ||
content-visibility: auto; | ||
} | ||
} */ | ||
|
||
input, button { | ||
@apply border-2; | ||
} | ||
</style> | ||
<script src="https://cdn.socket.io/4.5.0/socket.io.min.js" | ||
integrity="sha384-7EyYLQZgWBi67fBtVxw60/OWl1kjsfrPFcaU0pp0nAh+i8FD068QogUvg85Ewy1k" | ||
crossorigin="anonymous"></script> | ||
<script type="text/javascript" charset="utf-8"> | ||
var socket = io(); | ||
var USER = null; | ||
|
||
function getJwtToken() { | ||
return document.querySelector("#jwt_token").value; | ||
} | ||
|
||
function getToUser() { | ||
return document.querySelector("#to_user").value; | ||
} | ||
|
||
function getChatInput() { | ||
return document.querySelector("#chat_input").value; | ||
} | ||
|
||
function loginUser() { | ||
socket.emit("login", { jwt_token: getJwtToken() }, (response) => { | ||
console.log("Logged in!"); | ||
USER = response; | ||
}) | ||
} | ||
|
||
function clearChat() { | ||
var chat_history_element = document.querySelector('#chat_history'); | ||
chat_history_element.innerHTML = ""; | ||
} | ||
|
||
function appendChat(from, msg, left = true) { | ||
var div = document.createElement('div'); | ||
div.innerHTML += ` | ||
<div class="mx-3 my-2"> | ||
<div class="border-2 w-fit h-fit ${left === true ? "mr" : "ml"}-auto p-3 rounded-lg bg-blue-300 text-white"> | ||
<p>${from}</p> | ||
<p class="mt-2">${msg}</p> | ||
</div> | ||
</div> | ||
` | ||
|
||
var chat_history_element = document.querySelector('#chat_history'); | ||
chat_history_element.appendChild(div); | ||
} | ||
|
||
function scrollChatboxToBottom() { | ||
setTimeout(() => { | ||
const chatboxElement = document.getElementById("chat_box"); | ||
chatboxElement.scrollTop = chatboxElement.scrollHeight; | ||
}, 300); | ||
} | ||
|
||
function renderChat(chat) { | ||
if (chat.chat_from_system === true) { | ||
appendChat("SYSTEM", chat.chat_message, true); | ||
} else { | ||
if (USER.id === chat.chat_from_user_id) { | ||
appendChat(chat.chat_from_user_id, chat.chat_message, false); | ||
} else { | ||
appendChat(chat.chat_from_user_id, chat.chat_message, true); | ||
} | ||
} | ||
} | ||
|
||
function loadChat() { | ||
clearChat() | ||
|
||
loginUser() | ||
|
||
setTimeout(() => { | ||
socket.emit("chat_list", { | ||
"jwt_token": getJwtToken(), | ||
"to_user": getToUser(), | ||
"page": 1, | ||
"limit": 20, | ||
"sort": 1, | ||
"order_by": "send_date", | ||
"search": "1" | ||
}, | ||
(response) => { | ||
if (!response) return console.log("chat_list return null!"); | ||
|
||
const chat_history = response; | ||
// console.table(chat_history) | ||
|
||
clearChat(); | ||
|
||
chat_history.forEach((chat) => { | ||
renderChat(chat) | ||
// appendChat(`<li>${chat.chat_from_user_id} => ${chat.chat_to_user_id} : ${chat.chat_message}</li>`); | ||
}) | ||
|
||
scrollChatboxToBottom() | ||
}); | ||
}, 500); | ||
} | ||
|
||
function sendChat() { | ||
const chatMessage = getChatInput(); | ||
socket.emit("chat_send", { jwt_token: getJwtToken(), to_user: getToUser(), message: chatMessage }, (response) => { | ||
console.log("chat_send", response) | ||
}) | ||
} | ||
|
||
socket.on('connect', function () { | ||
console.log("Connected!"); | ||
}); | ||
|
||
socket.on('chat_receive', (response) => { | ||
console.log(response) | ||
const chat = response | ||
renderChat(chat) | ||
scrollChatboxToBottom() | ||
}); | ||
|
||
socket.on('exception', (response) => { | ||
console.error(response); | ||
}); | ||
</script> | ||
|
||
</html> |
Oops, something went wrong.