websocket updates

* sync page title on icon via websocket
* sync on page tree too
This commit is contained in:
Philipinho
2024-04-27 15:40:22 +01:00
parent 390d58793a
commit 8cc7d39146
17 changed files with 253 additions and 15 deletions

View File

@ -1,27 +1,44 @@
import {
OnGatewayConnection,
OnGatewayInit,
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
} from '@nestjs/websockets';
import { Server } from 'socket.io';
import { Server, Socket } from 'socket.io';
import { TokenService } from '../core/auth/services/token.service';
import { JwtType } from '../core/auth/dto/jwt-payload';
import { OnModuleDestroy } from '@nestjs/common';
@WebSocketGateway({ namespace: 'events' })
export class WsGateway implements OnGatewayInit, OnGatewayConnection {
@WebSocketGateway({
cors: { origin: '*' },
transports: ['websocket'],
})
export class WsGateway implements OnGatewayConnection, OnModuleDestroy {
@WebSocketServer()
server: Server;
constructor(private tokenService: TokenService) {}
async handleConnection(client: Socket, ...args: any[]): Promise<void> {
try {
const token = await this.tokenService.verifyJwt(
client.handshake.auth?.token,
);
if (token.type !== JwtType.ACCESS) {
client.disconnect();
}
} catch (err) {
client.disconnect();
}
}
@SubscribeMessage('message')
handleMessage(client: any, payload: any): string {
return 'Hello world!';
handleMessage(client: Socket, data: string): void {
client.broadcast.emit('message', data);
}
handleConnection(client: any, ...args: any[]): any {
//
}
afterInit(server: any): any {
//
onModuleDestroy() {
if (this.server) {
this.server.close();
}
}
}

View File

@ -1,7 +1,9 @@
import { Module } from '@nestjs/common';
import { WsGateway } from './ws.gateway';
import { AuthModule } from '../core/auth/auth.module';
@Module({
imports: [AuthModule],
providers: [WsGateway],
})
export class WsModule {}