(旁注:如果您不需要旧浏览器(或任何与此相关的浏览器,因为您的客户端是本机移动应用程序)的支持,那么您可以考虑使用 WebSocket,这是一个开放标准。Socket.io 通常用于具有在不支持 WebSocket 的浏览器上提供类似 WebSocket 的功能。另一方面,WebSocket 是一个开放标准,具有广泛的支持(不仅在浏览器中)并且具有更好的性能。请参阅这个答案 https://stackoverflow.com/questions/10112178/differences-between-socket-io-and-websockets/38558531#38558531更多细节。)
现在,既然您已经在使用Socket.io那么您可以通过以下方法诊断问题。我会尝试从浏览器进行连接,这是连接 Socket.io 的主要方式,然后看看是否有效。如果没有,则意味着您的服务器代码存在问题。如果是这样,则可能意味着您的客户端存在问题。这是首先要检查的事情。从这里开始,您可以缩小问题范围并有望解决它。
如果您想从使用 Socket.io 的一些工作代码开始,包括服务器站点 (Node.js) 和客户端(浏览器普通 JavaScript),那么您可以查看我最初编写的示例这个答案 https://stackoverflow.com/questions/10112178/differences-between-socket-io-and-websockets/38558531#38558531,可用的在 GitHub 上 https://github.com/rsp/node-websocket-vs-socket.io and on npm https://www.npmjs.com/package/websocket-vs-socket.io:
Socket.IO服务器
使用 Express.js 的 Socket.IO 服务器示例:
var path = require('path');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', (req, res) => {
console.error('express connection');
res.sendFile(path.join(__dirname, 'si.html'));
});
io.on('connection', s => {
console.error('socket.io connection');
for (var t = 0; t < 3; t++)
setTimeout(() => s.emit('message', 'message from server'), 1000*t);
});
http.listen(3002, () => console.error('listening on http://localhost:3002/'));
console.error('socket.io example');
Source: https://github.com/rsp/node-websocket-vs-socket.io/blob/master/si.js https://github.com/rsp/node-websocket-vs-socket.io/blob/master/si.js
Socket.IO客户端
使用普通 JavaScript 的 Socket.IO 客户端示例:
var l = document.getElementById('l');
var log = function (m) {
var i = document.createElement('li');
i.innerText = new Date().toISOString()+' '+m;
l.appendChild(i);
}
log('opening socket.io connection');
var s = io();
s.on('connect_error', function (m) { log("error"); });
s.on('connect', function (m) { log("socket.io connection open"); });
s.on('message', function (m) { log(m); });
Source: https://github.com/rsp/node-websocket-vs-socket.io/blob/master/si.html https://github.com/rsp/node-websocket-vs-socket.io/blob/master/si.html
您可以将相同的代码与 WebSocket 版本进行比较:
WebSocket服务器
使用 Express.js 的 WebSocket 服务器示例:
var path = require('path');
var app = require('express')();
var ws = require('express-ws')(app);
app.get('/', (req, res) => {
console.error('express connection');
res.sendFile(path.join(__dirname, 'ws.html'));
});
app.ws('/', (s, req) => {
console.error('websocket connection');
for (var t = 0; t < 3; t++)
setTimeout(() => s.send('message from server', ()=>{}), 1000*t);
});
app.listen(3001, () => console.error('listening on http://localhost:3001/'));
console.error('websocket example');
Source: https://github.com/rsp/node-websocket-vs-socket.io/blob/master/ws.js https://github.com/rsp/node-websocket-vs-socket.io/blob/master/ws.js
WebSocket客户端
使用普通 JavaScript 的 WebSocket 客户端示例:
var l = document.getElementById('l');
var log = function (m) {
var i = document.createElement('li');
i.innerText = new Date().toISOString()+' '+m;
l.appendChild(i);
}
log('opening websocket connection');
var s = new WebSocket('ws://'+window.location.host+'/');
s.addEventListener('error', function (m) { log("error"); });
s.addEventListener('open', function (m) { log("websocket connection open"); });
s.addEventListener('message', function (m) { log(m.data); });
Source: https://github.com/rsp/node-websocket-vs-socket.io/blob/master/ws.html https://github.com/rsp/node-websocket-vs-socket.io/blob/master/ws.html
我希望这可以帮助您评估使用 Socket.io 还是使用 WebSocket 对您来说是否是正确的决定,并为您提供一些有效的客户端代码来测试您的后端。代码是根据 MIT 许可发布 https://github.com/rsp/node-websocket-vs-socket.io#license(开源、免费软件)所以请随意在您的项目中使用它。