Mojolicious:我应该使用一个websocket还是几个? [英] Mojolicious: should I use one websocket or several?

查看:215
本文介绍了Mojolicious:我应该使用一个websocket还是几个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在教自己关于Mojolicious和websockets。到目前为止,我有一个显示数据库行的网页,并有按钮来添加,删除和更新行,以及选择要排序的列。

I'm teaching myself about Mojolicious and websockets. So far I've got a web page that displays rows from a database, and has buttons to add, delete, and update rows, and to select columns for sorting.

目前,它在每个按钮的javascriptonclick处理程序中使用一次性的websockets,并且可以正常工作。

At the moment, it uses 'one-shot' websockets in the javascript 'onclick' handlers for each button, and that works.

它是否更符合websockets的意图,是为了让套接字保持活跃并用于多次点击?我认为答案应该是'是',否则如果用户快速点击几次按钮就会变得混乱。

Would it be more in-keeping with the intention of websockets, for the sockets to be kept alive and used for multiple clicks? I think the answer should be 'yes', because otherwise it will get messy if the user clicks a button several times quickly.

而且,作为一个风格问题,应该我只有一个websocket来处理浏览器和服务器之间不同类型的交互,或者每个类型都有单独的websockets?拥有一个websocket需要代码来分析消息并决定如何处理它们。虽然几个websockets每个都更简单,但这需要重复错误处理代码等。

And, as a question of style, should I just have one websocket that handles different types of interaction between browser and server, or separate websockets for each type? Having one websocket would require code to analyse the messages and decide what to do about them. Whereas several websockets would each be simpler, but that would require repetition of error-handling code etc.

我知道这是一个罗嗦和哲学的问题,但我想要整体在我进一步开发之前应用程序的形状。

I know that's a wordy and philosophical question, but I want to get overall shape of the application right before I develop it further.

推荐答案

Ajax调用是客户端请求某些特定方法的有效方式来自服务器的信息采用一次性方法。

An Ajax call is an efficient way for a client to request some specific information from a server in a one-shot type of approach.

webSocket最适用于从客户端到服务器进行快速通信的情况(如此多请求您从持久连接中受益)或者您希望服务器能够随意向客户端发送数据(因为您必须具有持久连接才能将数据从服务器直接发送到客户端)。

A webSocket is best for a situation where you're doing rapid fire communication from client to server (so many requests that you benefit from a persistent connection) or when you want the server to be able to send data to the client at will (since you have to have a persistent connection to send data from server directly to client).

使用一次性webSocket(你创建webSocket,使用它然后关闭它)来偶尔请求从客户端到服务器的数据并不是最好的方法。因为设置webSocket还有额外的开销,因为两端协商双方是否支持webSockets并同意将协议从http更改为webSocket。

Using a one-shot webSocket (where you create the webSocket, use it and then close it) for an occasional request of data from client to server is not the most optimal way to do things because there is additional overhead to setting up the webSocket that is not present in the ajax call as the two ends negotiate whether both sides support webSockets and agree to change the protocol from http to webSocket.

一次性webSocket连接看起来像这样:

A one-shot webSocket connection would look like this:


  1. 客户端建立到服务器的TCP套接字

  2. 客户端使用webSocket升级头发送初始HTTP请求

  3. 服务器响应可以升级到webSocket协议

  4. 客户端发送消息

  5. 服务器收到消息并发送响应

  6. 客户端收到响应

  7. 客户端关闭TCP连接

  1. client establishes TCP socket to server
  2. client sends initial HTTP request with webSocket upgrade headers
  3. server responds that it is OK to upgrade to webSocket protocol
  4. client sends message
  5. server receives message and sends response
  6. client receives response
  7. client closes TCP connection

Ajax调用如下所示:

An Ajax call would look like this:


  1. 客户端建立到服务器的TCP套接字

  2. 客户端向服务器发送HTTP请求

  3. 服务器接收请求并发送响应

  4. 客户收到回复

  5. 客户关闭连接

  1. client establishes TCP socket to server
  2. client sends HTTP request to server
  3. server receives request and sends response
  4. client receives response
  5. client closes connection

并且,在浏览器中进行编码时,Ajax调用会自动关闭(因为它是专为一次性请求而设计的)所以客户甚至不必编码最后一步。

And, when coding in the browser, the Ajax call is automatically closed (since it is designed for one-shot requests) so the client doesn't even have to code that last step.

所以,如果你只是要求一些信息在最终用户点击时从服务器,然后似乎Ajax调用是一种更有效的方法(并且更容易实现)。

So, if you're just requesting some information from the server upon an end-user click, then it seems likely that an Ajax call is a more efficient approach (and simpler to implement).

这篇关于Mojolicious:我应该使用一个websocket还是几个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆