Socket.io客户端连接到服务器,但会立即触发;连接“U错误”;事件和连续重新连接
问题是socket.io客户端建立了到服务器的连接(在nginx反向代理之后),但是“connect\u error”回调会立即在客户端上触发。在本地主机上,服务器运行正常,但在服务器上,它没有建立正确的连接 其他信息:Socket.io客户端连接到服务器,但会立即触发;连接“U错误”;事件和连续重新连接,socket.io,nestjs,Socket.io,Nestjs,问题是socket.io客户端建立了到服务器的连接(在nginx反向代理之后),但是“connect\u error”回调会立即在客户端上触发。在本地主机上,服务器运行正常,但在服务器上,它没有建立正确的连接 其他信息: io = require("socket.io-client"); const socket = io.connect("https://api.messboot.at/socket.io", { transports: [&quo
io = require("socket.io-client");
const socket = io.connect("https://api.messboot.at/socket.io", {
transports: ["websocket"],
});
socket.on("connect_error", (error) => {
console.log("connection error!");
console.log(error);
});
import { Logger } from '@nestjs/common';
import { Socket, Server } from 'socket.io';
import {
MessageBody,
OnGatewayConnection,
OnGatewayDisconnect,
OnGatewayInit,
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
} from '@nestjs/websockets';
import { GpsDTO } from '@dto/gps.dto';
import { GpsService } from '@services/gps/gps.service';
interface GpsMessage {
timestamp_executed: string;
session_id: string;
latitude: number;
longitude: number;
speed: number;
accuracy: number;
}
@WebSocketGateway(8000, {
transport: ['websocket'],
allowUpgrades: false,
//namespace: '/socket.io/gps',
})
export class GPSGateway
implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
constructor(private service: GpsService) {}
private logger: Logger = new Logger('WebsocketGateway');
@WebSocketServer() wss: Server;
afterInit(server: Server) {
this.logger.log('Initialized:');
}
handleConnection(client: Socket) {
this.logger.log(`Client with id: ${client.id} connected!`);
}
handleDisconnect(client: Socket) {
this.logger.log(`Client with id: ${client.id} disconnected!`);
}
@SubscribeMessage('to_server')
handleMessage(@MessageBody() message: GpsMessage): void {
this.wss.emit('from_server', message);
const gps_data: GpsMessage = {
timestamp_executed: message.timestamp_executed,
session_id: message.session_id,
latitude: message.latitude,
longitude: message.longitude,
speed: message.speed,
accuracy: message.accuracy,
};
this.service.saveGpsDto(GpsDTO.from(gps_data));
console.log(message);
}
}
[Nest] 30 - 05/12/2021, 8:39:49 AM [WebsocketGateway] Client with id: FKwbAKhcTWvBFrPAAABM connected!
socket.io服务器和反向代理都是固定的。TCP握手似乎有效,但问题是“connect_error”回调立即被访问,客户端开始一次又一次地重新连接
客户端代码:
io = require("socket.io-client");
const socket = io.connect("https://api.messboot.at/socket.io", {
transports: ["websocket"],
});
socket.on("connect_error", (error) => {
console.log("connection error!");
console.log(error);
});
import { Logger } from '@nestjs/common';
import { Socket, Server } from 'socket.io';
import {
MessageBody,
OnGatewayConnection,
OnGatewayDisconnect,
OnGatewayInit,
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
} from '@nestjs/websockets';
import { GpsDTO } from '@dto/gps.dto';
import { GpsService } from '@services/gps/gps.service';
interface GpsMessage {
timestamp_executed: string;
session_id: string;
latitude: number;
longitude: number;
speed: number;
accuracy: number;
}
@WebSocketGateway(8000, {
transport: ['websocket'],
allowUpgrades: false,
//namespace: '/socket.io/gps',
})
export class GPSGateway
implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
constructor(private service: GpsService) {}
private logger: Logger = new Logger('WebsocketGateway');
@WebSocketServer() wss: Server;
afterInit(server: Server) {
this.logger.log('Initialized:');
}
handleConnection(client: Socket) {
this.logger.log(`Client with id: ${client.id} connected!`);
}
handleDisconnect(client: Socket) {
this.logger.log(`Client with id: ${client.id} disconnected!`);
}
@SubscribeMessage('to_server')
handleMessage(@MessageBody() message: GpsMessage): void {
this.wss.emit('from_server', message);
const gps_data: GpsMessage = {
timestamp_executed: message.timestamp_executed,
session_id: message.session_id,
latitude: message.latitude,
longitude: message.longitude,
speed: message.speed,
accuracy: message.accuracy,
};
this.service.saveGpsDto(GpsDTO.from(gps_data));
console.log(message);
}
}
[Nest] 30 - 05/12/2021, 8:39:49 AM [WebsocketGateway] Client with id: FKwbAKhcTWvBFrPAAABM connected!
Socket.io服务器代码:
io = require("socket.io-client");
const socket = io.connect("https://api.messboot.at/socket.io", {
transports: ["websocket"],
});
socket.on("connect_error", (error) => {
console.log("connection error!");
console.log(error);
});
import { Logger } from '@nestjs/common';
import { Socket, Server } from 'socket.io';
import {
MessageBody,
OnGatewayConnection,
OnGatewayDisconnect,
OnGatewayInit,
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
} from '@nestjs/websockets';
import { GpsDTO } from '@dto/gps.dto';
import { GpsService } from '@services/gps/gps.service';
interface GpsMessage {
timestamp_executed: string;
session_id: string;
latitude: number;
longitude: number;
speed: number;
accuracy: number;
}
@WebSocketGateway(8000, {
transport: ['websocket'],
allowUpgrades: false,
//namespace: '/socket.io/gps',
})
export class GPSGateway
implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
constructor(private service: GpsService) {}
private logger: Logger = new Logger('WebsocketGateway');
@WebSocketServer() wss: Server;
afterInit(server: Server) {
this.logger.log('Initialized:');
}
handleConnection(client: Socket) {
this.logger.log(`Client with id: ${client.id} connected!`);
}
handleDisconnect(client: Socket) {
this.logger.log(`Client with id: ${client.id} disconnected!`);
}
@SubscribeMessage('to_server')
handleMessage(@MessageBody() message: GpsMessage): void {
this.wss.emit('from_server', message);
const gps_data: GpsMessage = {
timestamp_executed: message.timestamp_executed,
session_id: message.session_id,
latitude: message.latitude,
longitude: message.longitude,
speed: message.speed,
accuracy: message.accuracy,
};
this.service.saveGpsDto(GpsDTO.from(gps_data));
console.log(message);
}
}
[Nest] 30 - 05/12/2021, 8:39:49 AM [WebsocketGateway] Client with id: FKwbAKhcTWvBFrPAAABM connected!
服务器日志:
io = require("socket.io-client");
const socket = io.connect("https://api.messboot.at/socket.io", {
transports: ["websocket"],
});
socket.on("connect_error", (error) => {
console.log("connection error!");
console.log(error);
});
import { Logger } from '@nestjs/common';
import { Socket, Server } from 'socket.io';
import {
MessageBody,
OnGatewayConnection,
OnGatewayDisconnect,
OnGatewayInit,
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
} from '@nestjs/websockets';
import { GpsDTO } from '@dto/gps.dto';
import { GpsService } from '@services/gps/gps.service';
interface GpsMessage {
timestamp_executed: string;
session_id: string;
latitude: number;
longitude: number;
speed: number;
accuracy: number;
}
@WebSocketGateway(8000, {
transport: ['websocket'],
allowUpgrades: false,
//namespace: '/socket.io/gps',
})
export class GPSGateway
implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
constructor(private service: GpsService) {}
private logger: Logger = new Logger('WebsocketGateway');
@WebSocketServer() wss: Server;
afterInit(server: Server) {
this.logger.log('Initialized:');
}
handleConnection(client: Socket) {
this.logger.log(`Client with id: ${client.id} connected!`);
}
handleDisconnect(client: Socket) {
this.logger.log(`Client with id: ${client.id} disconnected!`);
}
@SubscribeMessage('to_server')
handleMessage(@MessageBody() message: GpsMessage): void {
this.wss.emit('from_server', message);
const gps_data: GpsMessage = {
timestamp_executed: message.timestamp_executed,
session_id: message.session_id,
latitude: message.latitude,
longitude: message.longitude,
speed: message.speed,
accuracy: message.accuracy,
};
this.service.saveGpsDto(GpsDTO.from(gps_data));
console.log(message);
}
}
[Nest] 30 - 05/12/2021, 8:39:49 AM [WebsocketGateway] Client with id: FKwbAKhcTWvBFrPAAABM connected!
有人知道问题出在哪里吗?
提前感谢…问题在于客户端和服务器不兼容,因为socket.io的nestjs依赖项依赖于socket.io的版本2.x.x和客户端(socket.io的版本3)一次又一次地尝试重新连接,但直到我在Github上发现一个与此相关的问题之前,并没有明确的迹象表明为什么会发生这种情况。Nestjs已宣布在其下一个主要版本(V8)中支持socket.io版本3,直到有一个适配器代码可供使用: