RT server and signalr

This commit is contained in:
2026-02-15 20:33:08 +01:00
parent 6d8ae9bef6
commit cc47ac3a59
13 changed files with 214 additions and 1 deletions

View File

@@ -0,0 +1,46 @@
import {
HubConnection,
HubConnectionBuilder,
HubConnectionState,
} from "@microsoft/signalr";
export class SignalRService {
private connection: HubConnection;
constructor(
hubUrl: string,
) {
this.connection = new HubConnectionBuilder()
.withUrl(hubUrl)
.withAutomaticReconnect()
.build();
}
async start(): Promise<void> {
if (this.connection.state === HubConnectionState.Disconnected) {
await this.connection.start();
}
}
async stop(): Promise<void> {
if (this.connection.state !== HubConnectionState.Disconnected) {
await this.connection.stop();
}
}
on<T>(event: string, handler: (data: T) => void) {
this.connection.on(event, handler);
}
off(event: string) {
this.connection.off(event);
}
async invoke<T = void>(method: string, ...args: any[]): Promise<T> {
return await this.connection.invoke(method, ...args);
}
get state() {
return this.connection.state;
}
}

View File

@@ -0,0 +1,21 @@
import {SignalRService} from "@/services/signalr.ts";
const client = new SignalRService(
`http://localhost:5039/testhub`,
);
export const testHubService = {
async connect() {
await client.start();
},
/*
async joinBoard(boardId: string) {
await client.invoke("JoinBoard", boardId);
},
*/
onTest(callback: (text: string) => void) {
client.on<string>("ReceiveText", callback);
}
};