gpt4 book ai didi

javascript - 在将socket.io错误发送到控制台之前,我可以捕获它吗?

转载 作者:行者123 更新时间:2023-12-03 08:43:46 25 4
gpt4 key购买 nike

我在客户端上有一个非常简单的socket.io代码片段:

   setUpSockets () {
this.socket = io();
this.socket.on('beacon', (data) => {
console.log('DEBUG: beacon event received: ', data);
});
console.log("DEBUG: socket created: event handler ready ");
}

当我的服务器出现故障时,我开始出现此错误:

error pic

我特意使服务器停机以表征错误。我只想捕获一次并记录自己的错误1次。

这可能吗?

最佳答案

您可以利用各种经理事件,然后针对您的情况采取适当的措施。

如果客户端失去与服务器的连接,则不会得到它掉线的原因-例如服务器崩溃,客户端脱机等-您只会得到reconnecting事件,可以在客户端上点击并显示适当的消息传递。但是,您可以强制断开客户端与服务器的连接,并利用客户端的原因-查看下面包含的Socket disconnect事件。

以下是一些代码片段,您可以查看这些代码以查看可以挂接到的各种事件:

    let socket = io('http://localhost:3000', {autoConnect: false});

const enums = {
status: {
success: 'success',
fail: 'fail',
error: 'error',
warn: 'warn'
}
};

function log(message, when, status) {
status = status || 'success';

switch (status) {
case 'fail':
case 'error':
console.error(message);
break;
case 'success':
console.info(message);
break;
case 'warn':
console.warn(message);
break;
}
// additional logging etc
}

function registerEventHandlers() {
// manager events
//_________________________________________________________________________________________________
socket.io.on('connect_error', (err) => {
log(`manager:connect_error ${err.message}`, null, enums.status.error);
});

socket.io.on('connect_timeout', () => {
log(`manager:connect_timeout`, null, enums.status.error);
});

socket.io.on('reconnect_attempt', (attempt) => {
log(`manager:reconnect_attempt ${attempt}`, null, enums.status.warn);
});

socket.io.on('reconnecting', (attempt) => {
log(`manager:reconnecting ${attempt}`, null, enums.status.warn);
});

socket.io.on('reconnect_error', (err) => {
log(`manager:reconnect_error ${err.message}`, null, enums.status.error);
});

socket.io.on('reconnect_failed', () => {
log(`manager:reconnect_failed`, null, enums.status.error);
});
//_________________________________________________________________________________________________

// socket events
//_________________________________________________________________________________________________
socket.on('connect', () => {
authContainer.classList.add('hide');

log(`socket:connect ${socket.connected}`, null);
});

socket.on('connect_error', (err) => {
log(`socket:connect_error ${err.message}`, null, enums.status.error);
});

socket.on('connect_timeout', (timeout) => {
log(`socket:connect_timeout ${timeout}`, null, enums.status.error);
});

socket.on('error', (err) => {
if (err.type === 'UnauthorizedError' || err.code === 'invalid_token') {
log(`socket:error:invalid_token ${err.message}`, null, enums.status.error);
}

log(`socket:error ${err.message}`, null, enums.status.error);
});

socket.on('disconnect', (reason) => {
log(`socket:disconnect ${reason}`, null);
if (reason === 'io server disconnect') {
// the disconnection was initiated by the server
socket.close();

return reset();
}
// else the socket will automatically try to reconnect
});

socket.on('reconnect', (attempt) => {
log(`socket:reconnect ${attempt}`, null, enums.status.warn);
});

socket.on('reconnect_attempt', (attempt) => {
log(`socket:reconnect_attempt ${attempt}`, null, enums.status.warn);
});

socket.on('reconnecting', (attempt) => {
log(`socket:reconnecting ${attempt}`, null, enums.status.warn);
});

socket.on('reconnect_error', (err) => {
log(`socket:reconnect_error ${err.message}`, null, enums.status.error);
});

socket.on('reconnect_failed', () => {
log(`socket:reconnect_failed`, null, enums.status.error);
});
//_________________________________________________________________________________________________
}

socket.open();

关于javascript - 在将socket.io错误发送到控制台之前,我可以捕获它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59106233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com