Skip to content

Commit

Permalink
fix: 더 많은 브라우저에서 WebSocket 연결 문제가 없도록 수정 (#207)
Browse files Browse the repository at this point in the history
fix: 상대 url을 websocket에 사용할 수 있도록 폴리필
  • Loading branch information
hoqn authored Dec 4, 2024
1 parent 022635a commit 90b2d42
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/frontend/src/lib/polyfill-relative-urls-websocket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Monkey Patch로 WebSocket 생성자를 오버라이드
const OriginalWebSocket = window.WebSocket;

// @ts-expect-error Monkey Patch
window.WebSocket = function WebSocket(url, protocols) {
const parsedUrl = new URL(url, window.location.href);

if (parsedUrl.protocol === "http:") {
parsedUrl.protocol = "ws:";
} else if (parsedUrl.protocol === "https:") {
parsedUrl.protocol = "wss:";
}

return new OriginalWebSocket(parsedUrl.href, protocols);
};

window.WebSocket.prototype = OriginalWebSocket.prototype;
2 changes: 2 additions & 0 deletions packages/frontend/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";

import "@/lib/polyfill-relative-urls-websocket.ts";

import App from "./App.tsx";
import "./index.css";

Expand Down

0 comments on commit 90b2d42

Please sign in to comment.