简体   繁体   English

使用WebSockets ......有效吗?

[英]Using WebSockets… Efficiently?

I've read pretty much every guide and tutorial I can find on WebSockets, but not one of them has covered how to use them efficiently. 我已经阅读了很多我可以在WebSockets上找到的指南和教程,但其中没有一本已经介绍了如何有效地使用它们。

Does anyone have any sort of guide on how to do this? 有没有人有任何关于如何做到这一点的指南? I'm concerned about the amount of bandwidth a single connection can take up, especially when applications have hundrends of connections opened. 我担心单个连接可能占用的带宽量,特别是当应用程序打开了连接的趋势时。

The efficiency in WebSocket depends on the implementation and architecture of the webserver that handles them. WebSocket的效率取决于处理它们的Web服务器的实现和体系结构。 WebSocket is a very efficient protocol with only 2 byte (!) overhead, compare to the all different HTTP header fields and HTTP cookies that is sent in every Ajax request. WebSocket是一种非常有效的协议,只有2字节(!)开销,与每个Ajax请求中发送的所有不同HTTP头字段HTTP cookie相比

Usually an efficient webserver for websockets should be event driven (see the examples below). 通常,websockets的高效web服务器应该是事件驱动的(参见下面的示例)。 In opposite, the traditional way to implement web servers have been to spawn a new thread per request. 相反,实现Web服务器的传统方法是每个请求生成一个新线程。 But threads allocate much memory eg 256MB per thread. 但是线程分配了很多内存,例如每个线程256MB。 So if you have 1GB memory on your server you can't handle very many requests concurrently. 因此,如果服务器上有1GB内存,则无法同时处理很多请求。 But if your server is event-driven your memory usage will be almost constant, since new threads aren't created. 但是如果你的服务器是事件驱动的,你的内存使用率几乎是不变的,因为没有创建新的线程。 See also Technically why is processes in Erlang more efficient than OS threads? 另请参见技术上,为什么Erlang中的进程比OS线程更有效?

You can send any data in your WebSockets. 您可以在WebSockets中发送任何数据。 JSON is efficient if the client is a web browser, or maybe typed arrays . 如果客户端是Web浏览器或类型化数组 ,则JSON是高效的。 If your client is a custom application, you can use Protocol Buffers . 如果您的客户端是自定义应用程序,则可以使用Protocol Buffers See also Google Wave Client-Server Protocol Whitepaper 另请参阅Google Wave客户端 - 服务器协议白皮书

You can implement efficient websocket servers using Twisted (Python) or Netty (Java). 您可以使用Twisted (Python)或Netty (Java)实现高效的websocket服务器。 Play Framework is an efficient web framework for Java and Scala implemented on Netty. Play Framework是一个高效的Web框架,用于在Netty上实现的Java和Scala。 Another efficient alternative is Yaws web server (Erlang) or Node.js + Socket.io (JavaScript). 另一种有效的替代方案是Yaws web服务器(Erlang)或Node.js + Socket.io(JavaScript)。

As an application developer, the less data you send the more efficient (less traffic) and the less load on your server. 作为应用程序开发人员,发送的数据越少,效率越高(流量越少),服务器负载越少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM