mirror of
https://github.com/Shadowfita/docmost.git
synced 2025-11-12 07:42:34 +10:00
websocket updates
* sync page title on icon via websocket * sync on page tree too
This commit is contained in:
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 {}
|
||||
|
||||
Reference in New Issue
Block a user