Skip to content

Commit

Permalink
feat: ws url in prod
Browse files Browse the repository at this point in the history
  • Loading branch information
lideming committed Nov 26, 2023
1 parent 12d64ba commit 2eeff54
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion neetbox/frontend/src/pages/console/overview.tsx
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
2 changes: 1 addition & 1 deletion neetbox/frontend/src/pages/console/sidebar.tsx
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

Expand All @@ -19,7 +22,3 @@ export function useAPI(url: string, options?: { refreshInterval?: number }) {
}, [url, options?.refreshInterval]);
return useSWR(url, fetcher);
}

export function useWebSocketAPI() {

}
11 changes: 5 additions & 6 deletions neetbox/frontend/src/services/projectWebsocket.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { WEBSOCKET_URL } from "./api";
import { Project } from "./projects";

interface WsMsg<Type extends string = string, Payload = any> {
Expand All @@ -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({
Expand All @@ -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);
}
};
}
Expand All @@ -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);
}
}

0 comments on commit 2eeff54

Please sign in to comment.