Node.js socket.io-client connect_failed / connect_error事件

我在玩node.js和socket.io-client。 我试图连接到一个不存在的通道,以触发事件“connect_failed”(如https://github.com/LearnBoost/socket.io-client所指定)。

但是我不能得到事件的工作:

var clientio = require('socket.io-client'); console.log('Trying stuff ...'); // the channel does not exist var socket = clientio.connect( 'http://localhost:4000/news' ); // I expect this event to be triggered socket.on('connect_error', function(){ console.log('Connection Failed'); }); socket.on('connect', function(){ console.log('Connected'); }); socket.on('disconnect', function () { console.log('Disconnected'); }); socket.send('hi there'); 

如果我执行,会发生这种情况:

 $ node tmp_clientio.js Trying stuff ... 

如果连接到一个不存在的通道,有关如何触发错误的任何想法?

更新 :将connect_error重命名为connect_error

我遇到了同样的问题。 在我也碰到的命名空间模式之间有一个没有logging的区别,最后我不得不使用chromedebugging器来解决这个问题。

 // Bind to the news namespace, also get the underlying socket var ns_news = clientio.connect( 'http://localhost:4000/news' ); var socket = ns_news.socket 

所以,如上所述,如果要绑定“套接字级别”事件(如连接失败,断开连接等),则必须从命名空间中获取实际套接字 ns_news.socket

 // Global events are bound against socket socket.on('connect_failed', function(){ console.log('Connection Failed'); }); socket.on('connect', function(){ console.log('Connected'); }); socket.on('disconnect', function () { console.log('Disconnected'); }); // Your events are bound against your namespace(s) ns_news.on('myevent', function() { // Custom event code here }); 

请注意,您现在使用ns_news发送或接收事件

 ns_news.send('hi there'); 

最后,一定要绑定socket.on('error', ...)事件 – 如果端口不可用,就会发生

完全实现8080连接回退到端口80

我在我的实现(尝试端口8080,如果失败,尝试端口80通过nginx)

 socket_port = 8080; ns_dash = io.connect("http://your.gridspy.co.nz/dash", { port: socket_port, 'connect timeout': 5000, 'flash policy port': 10843 }); socket = ns_dash.socket; try_other_port = function() { if (socket_port !== 80) { if (typeof console !== "undefined" && console !== null) { console.log("Trying other port, instead of port " + socket_port + ", attempting port 80"); } socket_port = 80; socket.options.port = socket_port; socket.options.transports = ['htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']; return socket.connect(); } else { return typeof console !== "undefined" && console !== null ? console.log("No other ports to try.") : void 0; } }; socket.on('connect_failed', function() { if (typeof console !== "undefined" && console !== null) { console.log("Connect failed (port " + socket_port + ")"); } return try_other_port(); }); socket.on('error', function() { if (typeof console !== "undefined" && console !== null) { console.log("Socket.io reported a generic error"); } return try_other_port(); }); 

我还添加了一个nginx的proxy_pass规则,看起来像这样

 location ~ ^/(socket.io)/ { proxy_pass http://127.0.0.1:8080; proxy_http_version 1.1; proxy_set_header Host $http_host; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; } 

升级和连接选项(来自更新版本的nginx)需要升级nginx和socket.io之间的连接以支持websocket。 这意味着您的Web服务器可以支持端口80上的websockets。

此外,如果您的服务器支持ssl / https,这将代理wss,这是websockets的安全版本。

他们改变了事件名称。 使用

 reconnect_error 

要么

 connect_error 

代替

https://github.com/socketio/socket.io-client/blob/master/lib/socket.js#L27

从文档 :

连接失败

在最后一次连接尝试之后发生连接超时时触发。 只有在设置了connectTimeout选项时才会触发。 如果设置了tryTransportsOnConnectTimeout选项,则只有尝试了所有可能的传输时才会触发。

所以这个事件只有在设置了connectTimeout选项才会触发(这个实现在这里: https : //github.com/LearnBoost/socket.io-client/blob/master/lib/socket.js#L223-245 )。

所以你应该在你的代码中做不同的事情:

 io.connect('http://localhost:4000/news', { 'connect timeout': 5000 }); // set connect timeout to 5 seconds 

这适用于版本1.4.5(可能也适用于以前的版本)。

你想要做的是侦听socket.io客户端上的“错误”事件。 它会触发这个事件,错误信息将是“无效的命名空间”。

下面是一个如何从socket.io客户端处理它的例子:

  var ioclient = require('socket.io-client'); var websocket = ioclient('http://myserverurl.com/namespacethatdoesntexist'); websocket.on('error', function(err){ if (err == 'Invalid namespace') { console.warn("Attempted to connect to invalid namespace"); //handle the case here... } else { console.warn("Error on socket.io client", err); } });