哪个websocket库与Node.js一起使用? [关闭]

本文翻译自:Which websocket library to use with Node.js? [closed]

Currently there is a plethora of websocket libraries for node.js, the most popular seem to be: 目前node.js有大量的websocket库 ,最流行的似乎是:

However I can't find any solid concrete comparisons between any of them... Apparently Socket.io was awesome, but has become quite dated and has failing builds. 然而,我找不到任何一个之间的任何具体的具体比较...显然Socket.io很棒,但已经过时了,并且已经失败了。 Both ws and websocket-node claim they are the fastest. ws和websocket-node都声称它们是最快的。 And engine.io seems new, but a lot heavier than the lighter aletarntives. 而engine.io似乎是新的,但比较轻的aletarntives重得多。

It would be amazing if we or someone could put together an answer that serves as a guide on which socket library to use and when, as well as a comparison between them. 如果我们或某人能够将一个答案作为指导,使用哪个套接字库以及何时使用它们,以及它们之间的比较,那将是惊人的。


#1楼

参考:https://stackoom.com/question/16mNI/哪个websocket库与Node-js一起使用-关闭


#2楼

Getting the ball rolling with this community wiki answer. 通过这个社区维基回答滚动球。 Feel free to edit me with your improvements. 随意编辑我的改进。

  • ws WebSocket server and client for node.js. WS的WebSocket服务器和客户端的node.js中 One of the fastest libraries if not the fastest one. 如果不是最快的库之一,最快的库之一。

  • websocket-node WebSocket server and client for node.js websocket-node用于node.js的WebSocket服务器和客户端

  • websocket-driver-node WebSocket server and client protocol parser node.js - used in faye-websocket-node websocket-driver-node WebSocket服务器和客户端协议解析器node.js - 用于faye-websocket-node

  • faye-websocket-node WebSocket server and client for node.js - used in faye and sockjs faye-websocket-node用于node.js的WebSocket服务器和客户端 - 用于faye和sockjs

  • socket.io WebSocket server and client for node.js + client for browsers + (v0 has newest to oldest fallbacks, v1 of Socket.io uses engine.io) + channels - used in stack.io. socket.io用于browser.js的客户端和用于浏览器的客户端+(v0具有最新到最旧的回退,Socket.io的v1使用engine.io)+通道 - 在stack.io中使用。 Client library tries to reconnect upon disconnection. 客户端库尝试在断开连接时重新连接。

  • sockjs WebSocket server and client for node.js and others + client for browsers + newest to oldest fallbacks sockjs用于node.js的WebSocket服务器和客户端以及用于浏览器+最新到最旧回退的客户端

  • faye WebSocket server and client for node.js and others + client for browsers + fallbacks + support for other server-side languages 用于node.js和其他人的faye WebSocket服务器和客户端+用于浏览器+后备的客户端+支持其他服务器端语言

  • deepstream.io clusterable realtime server that handles WebSockets & TCP connections and provides data-sync, pub/sub and request/response deepstream.io可集群实时服务器,用于处理WebSockets和TCP连接,并提供数据同步,发布/ 订阅和请求/响应

  • socketcluster WebSocket server cluster which makes use of all CPU cores on your machine. socketcluster WebSocket服务器集群,它使用您计算机上的所有CPU核心。 For example, if you were to use an xlarge Amazon EC2 instance with 32 cores, you would be able to handle almost 32 times the traffic on a single instance. 例如,如果您要使用具有32个核心的xlarge Amazon EC2实例,则可以在单个实例上处理几乎32倍的流量。

  • primus Provides a common API for most of the libraries above for easy switching + stability improvements for all of them. primus为上面的大多数库提供了一个通用的API,以便为所有库提供简单的切换+稳定性改进。

When to use: 何时使用:

  • use the basic WebSocket servers when you want to use the native WebSocket implementations on the clientside, beware of the browser incompatabilities 当你想在客户端使用本机WebSocket实现时使用基本的WebSocket服务器,要小心浏览器的不兼容性

  • use the fallback libraries when you care about browser fallbacks 在关心浏览器回退时使用回退库

  • use the full featured libraries when you care about channels 在关心频道时使用全功能库

  • use primus when you have no idea about what to use, are not in the mood for rewriting your application when you need to switch frameworks because of changing project requirements or need additional connection stability. 当您不知道要使用什么时,请使用primus,当您因为更改项目要求或需要额外的连接稳定性而需要切换框架时,不会重写您的应用程序。

Where to test: 在哪里测试:

Firecamp is a GUI testing environment for SocketIO, WS and all major real-time technology. Firecamp是SocketIO,WS和所有主要实时技术的GUI测试环境。 Debug the real-time events while you're developing it. 在开发实时事件时调试它们。


#3楼

Update: This answer is outdated as newer versions of libraries mentioned are released since then. 更新:此答案已过时,因为此后提到的新版本的库已发布。

Socket.IO v0.9 is outdated and a bit buggy, and Engine.IO is the interim successor. Socket.IO v0.9已经过时且有点儿错误,而Engine.IO是临时继任者。 Socket.IO v1.0 (which will be released soon) will use Engine.IO and be much better than v0.9. Socket.IO v1.0(将很快发布)将使用Engine.IO并且比v0.9好很多。 I'd recommend you to use Engine.IO until Socket.IO v1.0 is released. 我建议您在发布Socket.IO v1.0之前使用Engine.IO。

"ws" does not support fallback, so if the client browser does not support websockets, it won't work, unlike Socket.IO and Engine.IO which uses long-polling etc if websockets are not available. “ws”不支持回退,所以如果客户端浏览器不支持websockets,它将无法工作,不像Socket.IO和Engine.IO使用长轮询等,如果websockets不可用。 However, "ws" seems like the fastest library at the moment. 然而,“ws”似乎是目前最快的图书馆。

See my article comparing Socket.IO, Engine.IO and Primus: https://medium.com/p/b63bfca0539 请参阅我的文章,比较Socket.IO,Engine.IO和Primus: https ://medium.com/p/b63bfca0539


#4楼

npm ws was the answer for me. npm ws是我的答案。 I found it less intrusive and more straight forward. 我发现它不那么具有侵入性和直接性。 With it was also trivial to mix websockets with rest services. 将websockets与休息服务混合在一起也很简单。 Shared simple code on this post. 这篇文章共享简单代码。

var WebSocketServer = require("ws").Server;
var http = require("http");
var express = require("express");
var port = process.env.PORT || 5000;

var app = express();
    app.use(express.static(__dirname+ "/../"));
    app.get('/someGetRequest', function(req, res, next) {
       console.log('receiving get request');
    });
    app.post('/somePostRequest', function(req, res, next) {
       console.log('receiving post request');
    });
    app.listen(80); //port 80 need to run as root

    console.log("app listening on %d ", 80);

var server = http.createServer(app);
    server.listen(port);

console.log("http server listening on %d", port);

var userId;
var wss = new WebSocketServer({server: server});
    wss.on("connection", function (ws) {

    console.info("websocket connection open");

    var timestamp = new Date().getTime();
    userId = timestamp;

    ws.send(JSON.stringify({msgType:"onOpenConnection", msg:{connectionId:timestamp}}));


    ws.on("message", function (data, flags) {
        console.log("websocket received a message");
        var clientMsg = data;

        ws.send(JSON.stringify({msg:{connectionId:userId}}));


    });

    ws.on("close", function () {
        console.log("websocket connection close");
    });
});
console.log("websocket server created");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值