尝试使用 socket.io 时出现错误

2024-04-02

我目前正在使用 socket.io swift 客户端。在 Iphone SE 上运行。这是快速代码

 let socket = SocketIOClient(socketURL: URL(string: "http://example.com:4000")!, config: [.log(true), .forcePolling(true)]);
        socket.connect();
        socket.on("connect") {data, ack in
            print("socket is connected");
            socket.emit("getData", ["data": 3]);
        }

在服务器上:

var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);

io.on('connection', function(socket){
    console.log('a user connected');
    socket.on('disconnect', function(){
        console.log('user disconnected');
    });
    socket.on('getData', function(result){
        console.log(result);
    });
});


app.listen(4000, function () {
  console.log(' on at 4000!');
});

...在 Xcode 控制台上,我得到

2016-09-29 16:38:33.871895 proj[3070:1019256] LOG SocketEngine: Handshaking
2016-09-29 16:38:33.872301 proj[3070:1019256] LOG SocketEnginePolling: Doing polling request
2016-09-29 16:38:34.004312 proj[3070:1019256] LOG SocketEnginePolling: Got polling response
2016-09-29 16:38:34.004874 proj[3070:1019283] LOG SocketEngine: Got message: Cannot GET /socket.io/?transport=polling&b64=1
2016-09-29 16:38:34.005283 proj[3070:1019283] ERROR SocketIOClient: Got unknown error from server Cannot GET /socket.io/?transport=polling&b64=1

这表明已建立连接并成功找到服务器,但还有其他问题。 将不胜感激任何帮助。


(旁注:如果您不需要旧浏览器(或任何与此相关的浏览器,因为您的客户端是本机移动应用程序)的支持,那么您可以考虑使用 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(开源、免费软件)所以请随意在您的项目中使用它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试使用 socket.io 时出现错误 的相关文章

随机推荐