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

Tk4*_*421 17 node.js

我正在玩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');
Run Code Online (Sandbox Code Playgroud)

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

$ node tmp_clientio.js 
Trying stuff ...
Run Code Online (Sandbox Code Playgroud)

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

更新:重命名connect_failedconnect_error

Tom*_*eys 16

我遇到了同样的问题.命名空间模式打开和关闭之间存在未记录的差异,我也遇到过,最后我不得不使用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
Run Code Online (Sandbox Code Playgroud)

因此,您可以看到,如果要绑定"套接字级别"事件(如连接失败,断开连接等),则必须从命名空间中获取实际套接字 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
});
Run Code Online (Sandbox Code Playgroud)

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

ns_news.send('hi there');
Run Code Online (Sandbox Code Playgroud)

最后,一定要绑定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();
});
Run Code Online (Sandbox Code Playgroud)

我还添加了一个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;
}
Run Code Online (Sandbox Code Playgroud)

此处的升级和连接选项(来自更新版本的nginx)是升级nginx和socket.io之间的连接以支持websockets所必需的.这意味着您的网络服务器可以支持端口80上的websockets.

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