Browse Source

changed cors

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

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

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

2
back/volume/src/main.ts

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

Loading…
Cancel
Save