简体   繁体   English

单页Web应用程序是否应该与服务器保持一个Web Socket连接?

[英]Should a single-page web application keep one Web Socket connection to the server or several?

I'm working on a single page Backbone application that is going to use web sockets. 我正在开发一个将使用Web套接字的单页Backbone应用程序。 The application is fairly complex, with 6 or more major areas (screens). 该应用程序相当复杂,有6个或更多主要区域(屏幕)。

The syntax of web sockets seems straightforward enough and I'm wondering now about the architecture. Web套接字的语法似乎很简单,我现在想知道架构。

When using web sockets, is it most performant to take the first approach or the 2nd approach? 使用网络套接字时,采用第一种方法或第二种方法是否最佳?

1 : Open a single websocket for all live server communication, on any screen or area of the application, and then filter those messages on the client side? 1 :在应用程序的任何屏幕或区域上为所有实时服务器通信打开单个websocket,然后在客户端过滤这些消息?

or... 要么...

2 : Open multiple websockets at a time, where each web socket represents some area of functionality in the application 2 :一次打开多个websockets,其中每个Web套接字代表应用程序中的某些功能区域

(I've seen this page, but it's about the server side and I'm interested in the client side: What is the best practice for WebSocket server(s)? ) (我看过这个页面,但它是关于服务器端的,我对客户端感兴趣: WebSocket服务器的最佳实践是什么?

Update: server is using Jetty (a Java technology not unlike Tomcat). 更新:服务器正在使用Jetty(一种与Tomcat不同的Java技术)。

I would open only one connection, easier to manage. 我只打开一个连接,更容易管理。 To ease debugging you could namespace all your events with the area name. 为了简化调试,您可以使用区域名称命名所有事件。 Also socket.io supports namespaces, see section "Restricting yourself to a namespace" here http://socket.io/#how-to-use . socket.io也支持名称空间,请参阅http://socket.io/#how-to-use “将自己限制在名称空间”部分。

Also if your doing lot of communications with your websocket you can turn it off with the Visibility API, example here: https://developer.mozilla.org/en-US/docs/DOM/Using_the_Page_Visibility_API 此外,如果您与websocket进行大量通信,可以使用Visibility API将其关闭,例如: https//developer.mozilla.org/en-US/docs/DOM/Using_the_Page_Visibility_API

Not a direct answer to your question but, since you say you're using backbone on the client, and assuming you're using node on the server (if not, this may be a compelling argument for doing so), you may be interested in this article which discusses in some detail the sharing of model data between client and (node) server to synchronise state. 不是你的问题的直接答案,但是,既然你说你在客户端使用骨干,并假设你在服务器上使用节点(如果没有,这可能是一个令人信服的论据),你可能会感兴趣本文详细讨论了客户端和(节点)服务器之间共享模型数据以同步状态。 Backbone, in case you weren't aware, can be installed as a module in node. 如果您不知道,Backbone可以作为模块安装在节点中。

Since your application is, as you describe, fairly complex, it seems likely that you will benefit from the ability to share the same complex model directly between client and server. 由于您所描述的应用程序非常复杂,因此您可能会直接在客户端和服务器之间共享相同的复杂模型。

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

相关问题 单页JavaScript Web应用程序的体系结构? - Architecture of a single-page JavaScript web application? 链接单页Web应用程序的各个部分 - Linking parts of a single-page web application 单页应用程序和网页标题 - Single-page application and web-page title 在单页pushState Web应用程序中模拟画外音页面加载 - Simulate Voiceover page load in single-page pushState web application 在单页网页设计上实现链接 - Implement links on single-page web design 使用Amazon Cognito的授权代码授予无服务器/单页Web应用程序 - Using Amazon Cognito's Authorization Code Grant with Serverless/Single-Page Web Application 如果未从根目录访问单页​​Web应用程序(SPA),则保证HTML信息 - Guaranteed HTML information if single-page web application (SPA) is not accessed at the root 单页网页应用中的Google Analytics跟踪问题 - Google Analytics trackevent in single-page web app Node.js中单页Web应用程序的推荐框架? - Recommended frameworks for single-page web apps in Node.js? 在单页Web应用程序中一次仅显示某些形式 - showing only certain form at a time in single-page web app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM