运行这个简单的 ping 测试时,我遇到了 25 秒的持续延迟。服务器没有记录ping事件,但我假设它正在接收它,因为客户端正在收到回复。关于如何解决这个问题的任何想法?
客户
var io = require('socket.io-client')('http://68.12.157.176:3000')
var ping_time = Date.now();
function ping(){
ping_time = Date.now();
console.log('sending ping...');
io.emit('ping');
}
ping();
io.on('pong', function (data) {
ping_time = Date.now() - ping_time;
console.log("replied in " + ping_time + "ms");
ping();
});
客户端日志
sending ping...
replied in 25202ms
sending ping...
replied in 25028ms
sending ping...
replied in 25029ms
sending ping...
replied in 25032ms
sending ping...
replied in 25016ms
服务器
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('../..')(server);
var port = process.env.PORT || 3000;
server.listen(port, function () {
console.log('Server listening at port %d', port);
});
io.on('connection', function (socket) {
console.log('client connected');
socket.on('ping', function (data) {
console.log("received ping, sending reply"); //no log shown???
socket.emit('pong'); //client receives event ~25 seconds after request
});
});
服务器日志
Server listening at port 3000
client connected
将我的评论变成答案,因为它解决了您的问题:
Socket.io 使用ping
和pong
作为消息名称本身在实现内部。更改为不同的邮件名称。