Skip to content

Commit

Permalink
can now do http:// & ws:// on the same port
Browse files Browse the repository at this point in the history
  • Loading branch information
robgruen committed Nov 19, 2024
1 parent 83cab64 commit cbcf831
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 22 deletions.
5 changes: 2 additions & 3 deletions ts/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ RUN apt install -y gnome-keyring

EXPOSE 80:3000
EXPOSE 8080:3000
EXPOSE 3443
EXPOSE 3030:3030
EXPOSE 3734
EXPOSE 443:3443
EXPOSE 8083:3443
CMD [ "pnpm", "start" ]
18 changes: 7 additions & 11 deletions ts/packages/api/src/typeAgentServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export class TypeAgentServer {

constructor(
private envPath: string,
private wsPort: number = 3030,
) {
// typeAgent config
dotenv.config({ path: this.envPath });
Expand All @@ -37,16 +36,6 @@ export class TypeAgentServer {
clientIO: this.webClientIO,
});

// websocket server
const hostEndpoint =
process.env["WEBSOCKET_HOST"] ?? `ws://localhost:${this.wsPort}`;
const url = new URL(hostEndpoint);
this.webSocketServer = new TypeAgentAPIWebSocketServer(
url,
this.dispatcher,
this.webClientIO!,
);

// web server config
const config: TypeAgentAPIServerConfig = JSON.parse(
readFileSync("data/config.json").toString(),
Expand All @@ -55,6 +44,13 @@ export class TypeAgentServer {
// web server
this.webServer = new TypeAgentAPIWebServer(config);
this.webServer.start();

// websocket server
this.webSocketServer = new TypeAgentAPIWebSocketServer(
this.webServer.server,
this.dispatcher,
this.webClientIO!,
);
}

stop() {
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/api/src/webServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type TypeAgentAPIServerConfig = {
};

export class TypeAgentAPIWebServer {
private server: Server<any, any>;
public server: Server<any, any>;
private secureServer: SecureServer<any, any> | undefined;

constructor(config: TypeAgentAPIServerConfig) {
Expand Down
9 changes: 4 additions & 5 deletions ts/packages/api/src/webSocketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@

import WebSocket, { WebSocketServer } from "ws";
import { Dispatcher } from "agent-dispatcher";
import { IncomingMessage } from "node:http";
import { IncomingMessage, Server } from "node:http";
import { WebAPIClientIO } from "./webClientIO.js";

export class TypeAgentAPIWebSocketServer {
private server: WebSocketServer;
private settingSummary: string = "";

constructor(
endpoint: URL,
webServer: Server<any, any>,
dispatcher: Dispatcher,
webClientIO: WebAPIClientIO,
) {
this.server = new WebSocketServer({
port: parseInt(endpoint.port),
path: endpoint.pathname,
server: webServer
});

this.server.on("listening", () => {
console.log(`WebSocket server started at ${endpoint}`);
console.log(`WebSocket server started!`);
process.send?.("Success");
});

Expand Down
2 changes: 1 addition & 1 deletion ts/packages/shell/src/renderer/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function getWebSocketAPI(): ClientAPI {

// TODO: update ws URI
let url = window.location;
createWebSocket(`ws://${url.hostname}:3030`, true).then(
createWebSocket(`ws://${url.hostname}:3000`, true).then(
(ws) => (globalThis.ws = ws),
);
}
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/shell/src/renderer/src/webSocketAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export async function createWebSocket(
// reconnect?
if (autoReconnect) {
let url = window.location;
createWebSocket(`ws://${url.hostname}:3030`, true).then(
createWebSocket(`ws://${url.hostname}:3000`, true).then(
(ws) => (globalThis.ws = ws),
);
}
Expand Down

0 comments on commit cbcf831

Please sign in to comment.