From 2eeff5483488f1222c2020a44aecaab322c6b035 Mon Sep 17 00:00:00 2001 From: lideming Date: Sun, 26 Nov 2023 18:08:56 +0800 Subject: [PATCH] feat: ws url in prod --- neetbox/frontend/src/pages/console/overview.tsx | 2 +- neetbox/frontend/src/pages/console/sidebar.tsx | 2 +- .../frontend/src/{hooks/useAPI.ts => services/api.ts} | 11 +++++------ neetbox/frontend/src/services/projectWebsocket.ts | 11 +++++------ 4 files changed, 12 insertions(+), 14 deletions(-) rename neetbox/frontend/src/{hooks/useAPI.ts => services/api.ts} (69%) diff --git a/neetbox/frontend/src/pages/console/overview.tsx b/neetbox/frontend/src/pages/console/overview.tsx index 835e852..4e0d5b2 100644 --- a/neetbox/frontend/src/pages/console/overview.tsx +++ b/neetbox/frontend/src/pages/console/overview.tsx @@ -1,5 +1,5 @@ import { useParams } from "react-router-dom"; -import { useAPI } from "../../hooks/useAPI"; +import { useAPI } from "../../services/api"; export default function Overview() { // const { projectName } = useParams(); diff --git a/neetbox/frontend/src/pages/console/sidebar.tsx b/neetbox/frontend/src/pages/console/sidebar.tsx index fee741b..c4cfb86 100644 --- a/neetbox/frontend/src/pages/console/sidebar.tsx +++ b/neetbox/frontend/src/pages/console/sidebar.tsx @@ -1,7 +1,7 @@ import { Nav } from "@douyinfe/semi-ui"; import React from "react"; import { IconStar, IconSetting } from "@douyinfe/semi-icons"; -import { useAPI } from "../../hooks/useAPI"; +import { useAPI } from "../../services/api"; import { Link, useLocation } from "react-router-dom"; export default function ConsoleNavBar() { diff --git a/neetbox/frontend/src/hooks/useAPI.ts b/neetbox/frontend/src/services/api.ts similarity index 69% rename from neetbox/frontend/src/hooks/useAPI.ts rename to neetbox/frontend/src/services/api.ts index a2635f5..fd60706 100644 --- a/neetbox/frontend/src/hooks/useAPI.ts +++ b/neetbox/frontend/src/services/api.ts @@ -1,10 +1,13 @@ import { useEffect } from "react"; import useSWR, { mutate } from "swr"; -const API_BASEPATH = "/web"; +export const API_BASEURL = "/web"; +export const WEBSOCKET_URL = import.meta.env.DEV + ? "ws://localhost:5001" + : `ws://${location.hostname}:${Number(location.port) + 1}}`; async function fetcher(url: string) { - const res = await fetch(API_BASEPATH + url); + const res = await fetch(API_BASEURL + url); return await res.json(); } @@ -19,7 +22,3 @@ export function useAPI(url: string, options?: { refreshInterval?: number }) { }, [url, options?.refreshInterval]); return useSWR(url, fetcher); } - -export function useWebSocketAPI() { - -} diff --git a/neetbox/frontend/src/services/projectWebsocket.ts b/neetbox/frontend/src/services/projectWebsocket.ts index 5f071ca..d66e273 100644 --- a/neetbox/frontend/src/services/projectWebsocket.ts +++ b/neetbox/frontend/src/services/projectWebsocket.ts @@ -1,3 +1,4 @@ +import { WEBSOCKET_URL } from "./api"; import { Project } from "./projects"; interface WsMsg { @@ -14,7 +15,7 @@ export class WsClient { nextLogId = 1; constructor(readonly project: Project) { - this.ws = new WebSocket("ws://127.0.0.1:5001/"); + this.ws = new WebSocket(WEBSOCKET_URL); this.ws.onopen = () => { console.info("ws open"); this.send({ @@ -33,7 +34,7 @@ export class WsClient { this.callbacks.delete(eventId); } else if (json["event-type"] === "log") { json.payload._id = this.nextLogId++; - project.handleLog(json.payload);; + project.handleLog(json.payload); } }; } @@ -45,10 +46,8 @@ export class WsClient { name: this.project.name, "event-id": eventId, }; - console.info('ws send', json); - this.ws.send( - JSON.stringify(json) - ); + console.info("ws send", json); + this.ws.send(JSON.stringify(json)); if (onReply) this.callbacks.set(eventId, onReply); } }