本文介绍了将主要 socket.io 升级(1.4.5 到 2.0)部署到生产环境的建议方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 socket.io 2.0 不向后兼容 (https://socket.io/blog/socket-io-2-0-0/) 我很好奇生产升级的建议程序是什么.

As socket.io 2.0 is not backward-compatible (https://socket.io/blog/socket-io-2-0-0/) I'm curious what is the suggested procedure of production upgrade.

问题是部署时有很多人连接到服务器,部署完成后他们很可能仍然使用旧的 socket.io 客户端尝试重新连接.

The problem is there are many people connected to a server when deployment takes place, and after deployment is completed they are still most likely using old socket.io client trying to reconnect.

在我的情况下,向后不兼容(1.4.5 -> 2.0)结果证明非常麻烦,因为旧客户端不断以疯狂的速度发送握手请求,而新服务器不断将它们推开,导致巨大的 CPU 和 ram 负载在服务器上.

Backward-incompatibility in my case (1.4.5 -> 2.0) turns out to be pretty troublesome as old client keeps sending handshake requests at an insane rate, and a new server keeps shoving them off resulting in huge cpu and ram loads on server.

在这种情况下建议的策略是什么(最好是长期的,即也照顾未来的更新)?这不是第一个向后不兼容的 socket.io 版本,我强烈感觉已经有好的做法在起作用.

What's suggested strategy in this case (preferably long-term, i.e. taking care of future updates too)? It's not first socket.io backwards-incompatible release and I have a strong feeling there already good practices at work.

先谢谢你,瓦西里·诺莫夫

Thank you in advance,Vasiliy Naumov

推荐答案

我可能会求助于在套接字上添加一个connect_error"侦听器,当 1.4.5 兼容的 socket.io 客户端时会触发解析器错误尝试连接到 2.0 socket.io 服务器,从那里我将重新加载页面并让浏览器加载新的 javascript,包括更新的 socket.io 客户端,如下所示:

I'll probably gonna resort to adding a "connect_error" listener on a socket, which is triggered with parser error when 1.4.5-compatible socket.io client tries to connect to 2.0 socket.io server, from there I'll reload the page and let the browser to load fresh javascript including a newer socket.io client like so:

socket.on('connect_error', function (error) {
  if (error.code === 'parser error') {
    // potential socket.io protocol update
    socket.io.reconnection(false); // stop reconnection attempts
    socket.disconnect();

    // additional check in case there is no actual socket.io client update, but rather some other reason for parser error being thrown
    var upgradeCheckedAlready = window.sessionStorage.getItem('socket.io-upgrade-check');
    if (!upgradeCheckedAlready) {
      window.sessionStorage.setItem('socket.io-upgrade-check', 1);
      window.location.reload();
    }
  }
})

这篇关于将主要 socket.io 升级(1.4.5 到 2.0)部署到生产环境的建议方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 11:57