Skip to content

Commit

Permalink
🧪 웹소켓 sockJs 사용 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaeng committed Dec 1, 2024
1 parent d02b85e commit 4fcac9a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
22 changes: 15 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@types/react": "^18.3.1",
"@types/react-dom": "^18.3.1",
"@types/react-router-dom": "^5.3.3",
"@types/sockjs-client": "^1.5.4",
"@types/styled-components": "^5.1.34",
"@typescript-eslint/eslint-plugin": "^8.11.0",
"@typescript-eslint/parser": "^8.11.0",
Expand Down
8 changes: 5 additions & 3 deletions src/apis/chat.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ChatEvent } from '@src/types/domain/dm';
import { Client, Frame, IMessage } from '@stomp/stompjs';
import SockJS from 'sockjs-client';

const WEBSOCKET_URL = process.env.REACT_APP_WEBSOCKET_URL!;
const WEBSOCKET_URL = process.env.REACT_APP_WEBSOCKET_URL_SECURE!;

let stompClient: Client | null = null;
type MessageHandler<T extends ChatEvent | MessageRequest> = (
Expand All @@ -16,13 +17,14 @@ export const connectHandler = <T extends ChatEvent | MessageRequest>(
const accessToken = localStorage.getItem('accessToken');

stompClient = new Client({
brokerURL: WEBSOCKET_URL,
// brokerURL: WEBSOCKET_URL,
connectHeaders: {
Authorization: `${accessToken}`.trim(),
},
forceBinaryWSFrames: true,
webSocketFactory: () => {
const ws = new WebSocket(WEBSOCKET_URL);
// const ws = new WebSocket(WEBSOCKET_URL);
const ws = new SockJS(WEBSOCKET_URL);

ws.onopen = () => {
console.log('[WebSocket Open]: Connection established');
Expand Down

0 comments on commit 4fcac9a

Please sign in to comment.