当前位置: 首页> 教育> 锐评 > 如何用源代码提取网页的图片_金融网络销售怎么找客源_网络媒体发稿_网站开发公司

如何用源代码提取网页的图片_金融网络销售怎么找客源_网络媒体发稿_网站开发公司

时间:2025/7/13 7:32:38来源:https://blog.csdn.net/qq_38983511/article/details/147156664 浏览次数:0次
如何用源代码提取网页的图片_金融网络销售怎么找客源_网络媒体发稿_网站开发公司

在 Node.js 中使用 TCP Socket 实现心跳机制(Heartbeat)的核心目的是检测连接是否存活,避免因网络波动或客户端异常断开导致的"僵尸连接"。以下是详细实现步骤及代码示例:


1. **基本原理

  • 心跳机制:服务端定期向客户端发送探测消息(如 ping),客户端需响应 pong
  • 超时检测:若在指定时间内未收到响应,判定连接失效并主动关闭。
  • 重置计时器:每次正常通信(收到数据或心跳响应)时,重置超时检测。

2. 服务端实现(使用 net 模块)

const net = require('net');// 创建 TCP 服务器
const server = net.createServer((socket) => {console.log('Client connected:', socket.remoteAddress);// 初始化心跳配置const HEARTBEAT_INTERVAL = 3000; // 3秒发送一次心跳const HEARTBEAT_TIMEOUT = 10000; // 10秒未响应则断开let heartbeatTimer = null;let timeoutTimer = null;// 发送心跳包const sendHeartbeat = () => {if (socket.writable) {socket.write('ping'); // 发送心跳标识console.log('Sent ping to client');}};// 启动心跳定时器const startHeartbeat = () => {heartbeatTimer = setInterval(sendHeartbeat, HEARTBEAT_INTERVAL);resetTimeout();};// 重置超时检测const resetTimeout = () => {if (timeoutTimer) clearTimeout(timeoutTimer);timeoutTimer = setTimeout(() => {console.log('Client timeout, closing connection');socket.destroy(); // 强制关闭连接}, HEARTBEAT_TIMEOUT);};// 监听客户端数据socket.on('data', (data) => {const message = data.toString();if (message === 'pong') {console.log('Received pong from client');resetTimeout(); // 收到响应,重置超时检测} else {console.log('Received data:', message);// 处理业务逻辑...}});// 启动心跳机制startHeartbeat();// 监听连接关闭socket.on('close', () => {console.log('Client disconnected');clearInterval(heartbeatTimer);clearTimeout(timeoutTimer);});// 错误处理socket.on('error', (err) => {console.error('Socket error:', err.message);});
});server.listen(3000, () => {console.log('Server listening on port 3000');
});

3. 客户端实现(Node.js 客户端示例)

const net = require('net');// 连接到服务端
const client = net.createConnection({ port: 3000 }, () => {console.log('Connected to server');
});// 监听服务端数据
client.on('data', (data) => {const message = data.toString();if (message === 'ping') {console.log('Received ping from server');client.write('pong'); // 响应心跳} else {console.log('Received data:', message);// 处理业务逻辑...}
});// 监听连接关闭
client.on('close', () => {console.log('Connection closed');
});// 错误处理
client.on('error', (err) => {console.error('Connection error:', err.message);
});

4. 关键逻辑说明

  1. 心跳发送

    • 服务端每隔 HEARTBEAT_INTERVAL(如 3 秒)发送 ping
    • 客户端需响应 pong,否则服务端会触发超时关闭。
  2. 超时检测

    • 每次发送心跳或收到正常数据时,重置超时计时器。
    • HEARTBEAT_TIMEOUT(如 10 秒)内未收到响应,判定连接失效。
  3. 资源清理

    • 连接关闭时,清除所有定时器(clearIntervalclearTimeout)。

5. 优化建议

  • 协议设计:使用固定格式(如 HEARTBEAT|pong)区分心跳与业务数据。
  • 重连机制:客户端检测到连接关闭后,可自动重连。
  • 动态配置:根据网络状况动态调整心跳间隔和超时时间。
  • TCP Keep-Alive:可结合系统级 Keep-Alive(需手动启用):
    socket.setKeepAlive(true, 60000); // 1分钟无活动发送探测包
    

6. 测试验证

  1. 启动服务端和客户端。
  2. 观察控制台日志,确认心跳正常(pingpong 交替出现)。
  3. 手动断开客户端网络,服务端应在 10 秒后检测到超时并关闭连接。

总结

通过应用层心跳机制,可以有效检测 TCP 连接的存活性,避免因意外断开导致的资源泄漏。实际项目中可根据需求调整心跳间隔和超时阈值,并结合业务逻辑完善错误处理。

关键字:如何用源代码提取网页的图片_金融网络销售怎么找客源_网络媒体发稿_网站开发公司

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: