Browse Source

changed cors

master
nicolas-arnaud 2 years ago
parent
commit
aaac161df0
  1. 96
      back/volume/src/chat/chat.gateway.ts
  2. 2
      back/volume/src/main.ts

96
back/volume/src/chat/chat.gateway.ts

@ -1,106 +1,100 @@
import { UnauthorizedException } from '@nestjs/common' import { UnauthorizedException, UseGuards } from "@nestjs/common";
import { import {
type OnGatewayConnection, type OnGatewayConnection,
type OnGatewayDisconnect, type OnGatewayDisconnect,
MessageBody, MessageBody,
SubscribeMessage, SubscribeMessage,
WebSocketGateway, WebSocketGateway,
WebSocketServer WebSocketServer,
} from '@nestjs/websockets' } from "@nestjs/websockets";
import { Socket, Server } from 'socket.io' import { Socket, Server } from "socket.io";
import { ChatService } from './chat.service' import { ChatService } from "./chat.service";
import { type User } from 'src/users/entity/user.entity' import { type User } from "src/users/entity/user.entity";
import { UsersService } from 'src/users/users.service' import { UsersService } from "src/users/users.service";
import { Channel } from './entity/channel.entity' import { Channel } from "./entity/channel.entity";
import { Message } from './entity/message.entity' import { Message } from "./entity/message.entity";
import { CreateChannelDto } from './dto/createChannel.dto' import { CreateChannelDto } from "./dto/createChannel.dto";
@WebSocketGateway({ @WebSocketGateway({
cors: { cors: { origin: /^(http|ws):\/\/localhost(:\d+)?$/ },
origin: [
'http://localhost:5000',
'http://localhost:80',
'http://localhost:8080'
]
}
}) })
export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
@WebSocketServer() @WebSocketServer()
server: Server server: Server;
constructor ( constructor(
private readonly userService: UsersService, private readonly userService: UsersService,
private readonly chatService: ChatService private readonly chatService: ChatService
) {} ) {}
async handleConnection (socket: Socket) { async handleConnection(socket: Socket) {
try { try {
const user: User | null = await this.userService.findUser( const user: User | null = await this.userService.findUser(
socket.data.user.ftId socket.data.user.ftId
) );
if (user == null) { if (user == null) {
socket.emit('Error', new UnauthorizedException()) socket.emit("Error", new UnauthorizedException());
// socket.disconnect(); // socket.disconnect();
return return;
} else { } else {
socket.data.user = user socket.data.user = user;
const channels = await this.chatService.getChannelsForUser(user.id) const channels = await this.chatService.getChannelsForUser(user.id);
// Only emit rooms to the specific connected client // Only emit rooms to the specific connected client
return this.server.to(socket.id).emit('channel', channels) return this.server.to(socket.id).emit("channel", channels);
} }
} catch { } catch {
socket.emit('Error', new UnauthorizedException()) socket.emit("Error", new UnauthorizedException());
// socket.disconnect(); // socket.disconnect();
} }
} }
handleDisconnect (socket: Socket) { handleDisconnect(socket: Socket) {
// socket.disconnect(); // socket.disconnect()
} }
@SubscribeMessage('createChannel') @SubscribeMessage("createChannel")
async onCreateChannel ( async onCreateChannel(
socket: Socket, socket: Socket,
@MessageBody() channeldto: CreateChannelDto @MessageBody() channeldto: CreateChannelDto
): Promise<Channel | null> { ): Promise<Channel | null> {
const channel = new Channel() const channel = new Channel();
channel.name = channeldto.name channel.name = channeldto.name;
const owner = await this.userService.findUser(channeldto.owner) const owner = await this.userService.findUser(channeldto.owner);
if (owner == null) return null if (owner == null) return null;
channel.owners.push(owner) channel.owners.push(owner);
channel.password = channeldto.password channel.password = channeldto.password;
/// .../// /// ...///
return await this.chatService.createChannel(channel, socket.data.user) return await this.chatService.createChannel(channel, socket.data.user);
} }
@SubscribeMessage('joinChannel') @SubscribeMessage("joinChannel")
async onJoinChannel (socket: Socket, channel: Channel) { async onJoinChannel(socket: Socket, channel: Channel) {
// add user to channel // add user to channel
const messages = await this.chatService.findMessagesInChannelForUser( const messages = await this.chatService.findMessagesInChannelForUser(
channel, channel,
socket.data.user socket.data.user
) );
this.server.to(socket.id).emit('messages', messages) this.server.to(socket.id).emit("messages", messages);
} }
@SubscribeMessage('leaveChannel') @SubscribeMessage("leaveChannel")
async onLeaveChannel (socket: Socket) { async onLeaveChannel(socket: Socket) {
await this.chatService.deleteBySocketId(socket.id) await this.chatService.deleteBySocketId(socket.id);
} }
@SubscribeMessage('addMessage') @SubscribeMessage("addMessage")
async onAddMessage (socket: Socket, message: Message) { async onAddMessage(socket: Socket, message: Message) {
const createdMessage: Message = await this.chatService.createMessage({ const createdMessage: Message = await this.chatService.createMessage({
...message, ...message,
author: socket.data.user author: socket.data.user,
}) });
const channel = await this.chatService.getChannel( const channel = await this.chatService.getChannel(
createdMessage.channel.id createdMessage.channel.id
) );
if (channel != null) { if (channel != null) {
const users = await this.userService.findOnlineInChannel(channel) const users = await this.userService.findOnlineInChannel(channel);
} }
/// TODO: Send message to users /// TODO: Send message to users
} }

2
back/volume/src/main.ts

@ -16,7 +16,7 @@ async function bootstrap (): Promise<void> {
? +process.env.BACK_PORT ? +process.env.BACK_PORT
: 3001 : 3001
const cors = { const cors = {
origin: ['http://localhost:80', 'http://localhost', '*'], origin: /^(http|ws):\/\/localhost(:\d+)?$/,
methods: 'GET, HEAD, PUT, PATCH, POST, DELETE, OPTIONS', methods: 'GET, HEAD, PUT, PATCH, POST, DELETE, OPTIONS',
preflightContinue: false, preflightContinue: false,
optionsSuccessStatus: 204, optionsSuccessStatus: 204,

Loading…
Cancel
Save