Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const logger = LoggerFactory.getLogger('WSGateway');
@WebSocketGateway(3002, {
path: '/',
// pingInterval: 30000, // default 25e3
// pingTimeout: 4000, // default 5e3
serveClient: false,
})
export class WSGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
@WebSocketServer()
private server: Server;
private readonly timestamp = Date.now();
@SubscribeMessage('events')
onHeartbeat(client: any, data: any): WsResponse {
console.log(data);
const event = 'events';
const response = `events-${pkg.version}-${this.timestamp}`;
return { event, data: response };
}
@SubscribeMessage('events2')
onEvents2(client: any, data: any): WsResponse {
console.log(data);
const event = 'events';
const response = `events-${pkg.version}-${this.timestamp}`;
return { event, data: response };
}
public afterInit(server: any): any {
client: Socket,
data: { token: string; data: { id: string } }
) {
try {
const response = await this.amendService.downVoteAmend(
data.data.id,
data.token,
this.io
)
client.emit('downVoteAmend', response)
} catch (error) {
console.error(error)
}
}
@SubscribeMessage('indVoteAmend')
async handleIndVoteAmend(
client: Socket,
data: { token: string; data: { id: string } }
) {
try {
const response = await this.amendService.upVoteAmend(
data.data.id,
data.token,
this.io
)
client.emit('indVoteAmend', response)
} catch (error) {
console.error(error)
}
}
}
@UseGuards(EventAuthGuard)
@UsePipes(SocketAuthPipe)
@SubscribeMessage(GameEvents.ChangePlaylist)
async onChangePlaylist(client: Socket, { data, user }) {
const game = await this.gameService.setPlaylist(user, data.playlist, data.lang)
this.server.to(game.key).emit(GameEvents.ChangePlaylist, new PlayerGameInfoDto(game))
return new GameDto(game)
}
@UseGuards(EventAuthGuard)
@UsePipes(SocketAuthPipe)
@SubscribeMessage(GameEvents.Start)
async onStart(client: Socket, { data, user }) {
const key = data.key
let game = await this.gameService.get(key)
game = await this.gameService.setState(game.key, GameState.Paused)
await this.spotify.pausePlayback(user)
const gameUpdate: Partial = {
state: game.state
}
this.server.to(data.key).emit(GameEvents.Start, gameUpdate)
console.log(`[${key}] Start game`)
return new GameDto(game)
}
@SubscribeMessage(GameEvents.Join)
async onJoin(client: Socket, userId) {
const game = await this.playerService.connect(userId, client.id)
client.join(game.key)
const gameUpdate: Partial = {
players: game.players
}
this.server.to(game.key).emit(GameEvents.Update, gameUpdate)
console.log(`[${game.key}] User ${userId} joined`)
return new PlayerGameInfoDto(game)
}
@SubscribeMessage(GameEvents.Buzz)
async onBuzz(client: Socket, userId) {
const game: Game = await this.gameService.getByPlayerId(userId)
if (game.state !== GameState.Playing) return
this.server.to(game.key).emit(GameEvents.Pause)
this.gameService.setState(game.key, GameState.Paused)
this.server.to(game.host.socket).emit(GameEvents.Buzzed, userId)
this.spotify.pausePlayback(game.host.user)
console.log(`[${game.key}] User ${userId} buzzed`)
}
@SubscribeMessage(GameEvents.Leave)
async onLeave(client: Socket) {
const game = await this.playerService.leave(client.id)
@WebSocketGateway({ port: 1314 })
export class AppGeteway {
@WebSocketServer() server;
constructor(private hunli: HunliDiscussService, private fuyue: HunliFuyueService) {
}
@SubscribeMessage('hunli.discuss')
async handelHunliDiscuss(sender, data) {
const list = await this.hunli.findAll();
sender.emit('hunli.discuss', list);
}
@SubscribeMessage('hunli.discuss.add')
async handelHunliDiscussAdd(sender, data) {
const item = await this.hunli.addOne(data);
const list = await this.hunli.findAll();
sender.broadcast.emit('hunli.discuss.add', data);
sender.emit('hunli.discuss.add', data);
}
@SubscribeMessage('hunli.fuyue')
async handelHunliFuyue(sender, data) {
const list = await this.fuyue.findAll();
sender.emit('hunli.fuyue', list);
}
@SubscribeMessage('hunli.fuyue.add')
async handelHunliFuyueAdd(sender, data) {
const item = await this.fuyue.addOne(data);
import { withTryCatch, withAuthentication, withResponse } from '../common'
import { IMessage } from '../../../interfaces'
@WebSocketGateway()
export class AmendGateway {
constructor(
@Inject('AuthService')
private readonly authService: AuthService,
@Inject('AmendService')
private readonly amendService: AmendService
) {}
@WebSocketServer()
io: Server
@SubscribeMessage('amend')
@withTryCatch
async handleAmend(client: Socket, message: IMessage<{ id: string }>) {
const response = await this.amendService.getAmend(message.data.id)
client.emit('amend/' + message.data.id, response)
}
@SubscribeMessage('postAmend')
@withResponse('postAmend')
@withTryCatch
@withAuthentication
async handlePostAmend(
client: Socket,
message: IMessage<{
name: string
description: string
patch: string
@SubscribeMessage('hunli.discuss')
async handelHunliDiscuss(sender, data) {
const list = await this.hunli.findAll();
sender.emit('hunli.discuss', list);
}
@SubscribeMessage('hunli.discuss.add')
async handelHunliDiscussAdd(sender, data) {
const item = await this.hunli.addOne(data);
const list = await this.hunli.findAll();
sender.broadcast.emit('hunli.discuss.add', data);
sender.emit('hunli.discuss.add', data);
}
@SubscribeMessage('hunli.fuyue')
async handelHunliFuyue(sender, data) {
const list = await this.fuyue.findAll();
sender.emit('hunli.fuyue', list);
}
@SubscribeMessage('hunli.fuyue.add')
async handelHunliFuyueAdd(sender, data) {
const item = await this.fuyue.addOne(data);
if (item) {
const list = await this.fuyue.findAll();
sender.broadcast.emit('hunli.fuyue.add', data);
sender.emit('hunli.fuyue.add', data);
}
}
}
private subject: ReplaySubject = new ReplaySubject(10, 1000 * 60 * 2);
private lastChatNotificationEmail: Date;
constructor(@Inject('MailService') private mailService: MailService) {}
afterInit(server: Server): void {
this.server = server;
this.subject.subscribe(message => {
this.server.clients.forEach(client => {
client.send(JSON.stringify({ event: 'chat', data: message }));
});
});
}
@SubscribeMessage('chat')
onMessage(client, message: ChatMessage): void {
message.timestamp = new Date();
this.notifyChatAction(message);
this.subject.next(message);
}
private notifyChatAction(message: ChatMessage): void {
const msg: string = JSON.stringify(message);
Logger.log('chat action going on: ' + msg);
const oneHourEarlier: Date = new Date();
oneHourEarlier.setTime(oneHourEarlier.getTime() - 60 * 60 * 1000);
if (this.lastChatNotificationEmail == null || this.lastChatNotificationEmail < oneHourEarlier) {
this.mailService.sendEmail(null, null, 'GEOLUD-SITE: Chat actions going on ...', msg);
}
this.lastChatNotificationEmail = new Date();
}
async handleDeleteAccount(client: Socket, message: IMessage<{}>) {
return await this.userService.delete(message.data, this.io)
}
@SubscribeMessage('updateEmail')
@withResponse('updateEmail')
@withTryCatch
@withAuthentication
async handleUpdateEmail(
client: Socket,
message: IMessage<{ email: string }>
) {
return await this.userService.updateEmail(message.data)
}
@SubscribeMessage('updatePassword')
@withResponse('updatePassword')
@withTryCatch
@withAuthentication
async handleUpdatePassword(
client: Socket,
message: IMessage<{ password: string }>
) {
return await this.userService.updatePassword(message.data)
}
@SubscribeMessage('updateLastEvent')
@withResponse('user')
@withTryCatch
@withAuthentication
async handleUpdateLastEvent(client: Socket, message: IMessage<{}>) {
return await this.userService.updateLastEventDate(message.data)
@SubscribeMessage(WS_SUBSCRIBE_EVENTS.TO_CORPORATION_WALL)
async onSubscribeToCorporationWallEvent(client: ISocket, data: any): Promise {
return this.onSubscribeEvent(client, data);
}
@SubscribeMessage(WS_SUBSCRIBE_EVENTS.TO_ALLIANCE_WALL)
async onSubscribeToAllianceWallEvent(client: ISocket, data: any): Promise {
return this.onSubscribeEvent(client, data);
}
@SubscribeMessage(WS_SUBSCRIBE_EVENTS.TO_POST_COMMENTS)
async onSubscribeToPostCommentsEvent(client: ISocket, data: any): Promise {
return this.onSubscribeEvent(client, data);
}
@SubscribeMessage(WS_UN_SUBSCRIBE_EVENTS.FROM_LATEST_WALL)
async onUnSubscribeFromLatestWallEvent(client: ISocket, data: any): Promise {
return this.onUnSubscribeEvent(client, data);
}
@SubscribeMessage(WS_UN_SUBSCRIBE_EVENTS.FROM_HASHTAG_WALL)
async onUnSubscribeFromHashtagWallEvent(client: ISocket, data: any): Promise {
return this.onUnSubscribeEvent(client, data);
}
@SubscribeMessage(WS_UN_SUBSCRIBE_EVENTS.FROM_CHARACTER_WALL)
async onUnSubscribeFromCharacterWallEvent(client: ISocket, data: any): Promise {
return this.onUnSubscribeEvent(client, data);
}
@SubscribeMessage(WS_UN_SUBSCRIBE_EVENTS.FROM_CORPORATION_WALL)
async onUnSubscribeFromCorporationWallEvent(client: ISocket, data: any): Promise {