Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/constants/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ export const GAME2_TASK_API = process.env.REACT_APP_GAME2_TASK_API;
export const GAME2_EVALUATE_API = process.env.REACT_APP_GAME2_EVALUATE_API;

// 채팅 소켓 API
export const SOCKET_CHAT_ALL = process.env.REACT_APP_WS_CHAT_ALL_API;
export const SOCKET_CHAT_ALL_SUB = process.env.REACT_APP_WS_CHAT_ALL_SUB_API;
export const SOCKET_CHAT_ALL_PUB = process.env.REACT_APP_WS_CHAT_ALL_PUB_API;
export const SOCKET_CHAT_PRIVATE = process.env.REACT_APP_WS_CHAT_PRIVATE_API;
14 changes: 8 additions & 6 deletions src/hooks/lobby/useChatSocket.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import { SOCKET_CHAT_PRIVATE, SOCKET_CHAT_ALL } from "constants/api";
import {
SOCKET_CHAT_PRIVATE,
SOCKET_CHAT_ALL_PUB,
SOCKET_CHAT_ALL_SUB,
} from "constants/api";
import { useEffect, useRef, useState } from "react";
import { useGameSocketStore } from "store/socket";
import { replaceUuid } from "utils/filter";
import { getUserName } from "utils/user";

const useChatSocket = ({ userUuid }) => {
const { stompClient, isConnected } = useGameSocketStore();
const clientRef = useRef(null);

const [messages, setMessages] = useState([]);
const [isRateLimited, setIsRateLimited] = useState(false);
const [sendTimestamps, setSendTimestamps] = useState([]);

const myName = getUserName();
useEffect(() => {
if (!isConnected || !userUuid) return;

console.log("채팅 WebSocket 연결됨");

const chatGlobalSub = stompClient.subscribe(
`/sub${SOCKET_CHAT_ALL}`,
`${replaceUuid(SOCKET_CHAT_ALL_SUB, userUuid)}`,
(message) => {
const body = JSON.parse(message.body);
console.log(body);
Expand Down Expand Up @@ -90,10 +93,9 @@ const useChatSocket = ({ userUuid }) => {
stompClient.publish({
destination: receiverNickname
? `/pub${SOCKET_CHAT_PRIVATE}`
: `/pub${SOCKET_CHAT_ALL}`,
: `/pub${SOCKET_CHAT_ALL_PUB}`,
body: JSON.stringify(payload),
});

setSendTimestamps([...recent, now]);
return true;
};
Expand Down
5 changes: 4 additions & 1 deletion src/utils/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ const getLastLocationPath = (path) => {
return pathArr[pathArr.length - 1];
};

export { getLastLocationPath };
const replaceUuid = (API, userUuid) => {
return API.replace("UUID", userUuid);
};
export { getLastLocationPath, replaceUuid };