Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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_Failed重命名为connect_error

解决方法

我遇到同样的问题.命名空间模式开启和关闭之间有一个未记录的差异,我也遇到了,最后我不得不使用chrome调试器来处理它.

// 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('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,如果失败,尝试通过Nginx端口80)

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之间的连接以支持Websockets.这意味着您的Web服务器可以支持端口80上的Websockets.

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

大佬总结

以上是大佬教程为你收集整理的Node.js socket.io-client connect_failed / connect_error事件全部内容,希望文章能够帮你解决Node.js socket.io-client connect_failed / connect_error事件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。