douqulv6059
douqulv6059
2015-04-14 02:05

让socket.io客户端版本落后于服务器版本

  • socket.io
  • websocket
  • sockets
已采纳

Situation

We're using socket.io for mobile-server communications. Since we can't force-upgrade users' devices, if we want to upgrade to version 1 (non-back-compatible), we have to handle both versions on the server for a while.

Question

What are the options?

My current favourite is to wrap both the old version and the new version in a multiplexer. It detects the version of the incoming request based on headers and query parameters and thereby knows which functions to invoke.

Another (shittier) option is to wrap the new version in a module that can translate the old version of the protocol into the new version (and back again) when necessary. This suffers from a serious drawback. It would be time-consuming and uncertain work to ensure I've properly determined and handled all the tiny differences. Some differences might take some serious massaging.

(In case you're curious or it's helpful to know, we're doing this in Go.)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dongxia4880 dongxia4880 6年前

    We're going to go the route of keeping both the 0.9.x version and the current version as separate libraries on the server. Eventually, when the pool of clients has more-or-less all updated, we'll just pull the plug on the 0.9.x version.

    The way we'll manage the two versions is by wrapping the socket.io services in a package that will determine which wrapped socket.io version to pass the request off to. This determination will depend on features of the request, such as custom headers (that can be added to the newer clients) as well as query parameters and other headers utilized exclusively by one version or the other.

    Since we're using Go, there's so far no universally agreed upon way to manage dependencies, let alone a way that can respect version differences. Assuming the back-compat branch of the repo wasn't broken (which it is), we'd have two options. The first would be to fork the repo and make the back-compat version the master. We'd then import it as if it had nothing to do with the other one. The second option would be to use gopkg.in to pretend the separate branches were separate repos.

    In either event, we could import the two branches/repos like so

    import (
        socketioV0 "github.com/path/to/older/version"
        socketioV1 "github.com/path/to/current/version"
    )
    

    And then refer to them in the code using their import names socketioV0 and socketioV1.

    点赞 评论 复制链接分享
  • doudui5753 doudui5753 6年前

    It appears that you could run two separate versions of socket.io on the server. Since the two versions don't have unique module filenames you would probably need to load one version from a different path. And, then obviously when loading the modules and initializing them you'd assign them to differently named variables. For example:

    var io_old = require('old/socket.io');
    var io = require('socket.io);
    

    Once you have the two versions loaded on the server, I think there are two different approaches for how they could be run.

    1) Use a different port for each version. The older version would use the default port 80 (no configuration change required for that) which is shared with the node.js web server. The newer version would be run on a different port (say port 3000). You would then initialize each version of socket.io to its own port. Your newer version clients would then connect to the the port the newer version was running on.

    For the old socket.io server running on port 80, you would use whatever initialization you already have which probably hooks into your existing http server.

    For the new socket.io server running on some other port, you would initialize it separately like this:

    var io_old = require('old/socket.io')(server);
    var io = require('socket.io')(3000);
    

    Then, in the new version client, you would specify port 3000 when connecting.

    var socket = io("http://yourdomain.com:3000");
    

    2) Use a different HTTP request path for each version. By default, each socket.io connection starts with an HTTP request that looks like this: http://yourdomain.com/socket.io?EIO=xx&transport=xxx?t=xxx. But, the /socket.io portion of that request is configurable and two separate versions of socket.io could each be using a different path name. On the server, the .listen() method that starts socket.io listening takes an optional options object which can be configured with a custom path as in path: "/socket.io-v2". And similarly, the .connect() method in the client also accepts that options object. It's kind of hard to find the documentation for this option because it's actually an engine.io option (which socket.io uses), but socket.io passes the options through to engine.io.

    I have not tried either of these myself, but I've studied how socket.io connections are initiated from client and server and it looks like the underlying engine supports this capability and I can see no reason why it should not work.

    Here's how you'd change the path on the server:

    var io = require('socket.io')(server, {path: "/socket.io.v1"});
    

    Then, in the client code for the new version, you'd connect like this:

    var socket = io({path: "/socket.io.v1"});
    

    This would then result in the initial connection request being made to an HTTP URL like this:

    http://yourdomain.com/socket.io.v1?EIO=xx&transport=xxx?t=xxx

    Which would be handled by a different request handler on your HTTP server, thus separating the two version.


    FYI, it is also possible that the EIO=3 query parameter in the socket.io connection URL is actually an engine.io version number and that can also be used to discern client version and "do the right thing" based on that value. I have not found any documentation on how that works and could not even find where that query parameter was looked at in the engine.io or socket.io source code so that would take more investigation as a another possibility.

    点赞 评论 复制链接分享
  • dragon87836215 dragon87836215 6年前

    I don't really have an immediate solution for this, but I have some kind of advice. I guess you could use it to save a lot of time.

    • first of all Im working in a startup which uses socketIo for almost
      everything
    • We knew that this problem would happen so our initial design was to
      make everything pluggable which means that we can swap out socketio for
      sockjs and it will still work.
    • The way its done is by defining the common set of APIs which rarely change
      in a system. We call it managers. The managers can just expose the API which the rest of the devs need to use without messing up anything. It speeds up a lot.
    • The manager implementation changes in the background but still the APIs are the same, so the engineers working on the core can confidently make changes.
    • Seems like you have a tight dependency in your code. Or may be not. I'm not so sure. Try following this principle if you haven't.
    点赞 评论 复制链接分享

为你推荐