Skip to content

Commit

Permalink
Merge pull request #62 from tscenping/jiyun
Browse files Browse the repository at this point in the history
feat: socket.io cors errror solving
  • Loading branch information
tomatozil authored Dec 7, 2023
2 parents 580c439 + f4f85fd commit 8629d86
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/socket-adapter/socket-io.adapter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IoAdapter } from '@nestjs/platform-socket.io';
import { INestApplicationContext } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { Server, Socket } from 'socket.io';
import { Server, Socket, ServerOptions } from 'socket.io';
import { WSUnauthorizedException } from '../common/exception/custom-exception';
import { UsersRepository } from '../users/users.repository';
import { User } from '../users/entities/user.entity';
Expand All @@ -22,8 +22,24 @@ export class SocketIoAdapter extends IoAdapter {
const jwtService = this.app.get(JwtService);
const configService = this.app.get(ConfigService);
const userRepository = this.app.get(UsersRepository);
const clientPort = '8001';

const server: Server = super.createIOServer(port, options);
const cors = {
credentials: true,
origin: [
`http://localhost:${clientPort}`,
new RegExp(
`/^http:\/\/192\.168\.1\.([1-9]|[1-9]\d):${clientPort}$/`,
),
],
};

const optionsWithCORS: ServerOptions = {
...options,
cors,
};

const server: Server = super.createIOServer(port, optionsWithCORS);

const namespaces = ['channels'];

Expand Down

0 comments on commit 8629d86

Please sign in to comment.