使用Electron(Atom Shell)时的客户端/服务器模型是什么? [英] What is the Client/Server model when using Electron (Atom Shell)?

查看:104
本文介绍了使用Electron(Atom Shell)时的客户端/服务器模型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解 Electron (以前称为Atom Shell)的工作方式。

I'm trying to wrap my head around how Electron (formerly Atom Shell) works.

我来自传统的MVC风格的Web应用程序,其中浏览器通过路由系统调用 Controller Action 然后从商店(文件系统,数据库,...)获取数据并呈现视图,然后将其发送回浏览器。有些动作可能会发回JSON,因为它们是通过JavaScript / AJAX调用的,而不是实际导航到它们的浏览器。

I'm coming from a traditional, MVC-style web application where a Browser is calling a Controller Action through a Routing System, the Controller then fetches data from a store (File System, Data Base, ...) and renders a View, which is sent back to the Browser. Some Actions may be sending back JSON instead, as they are called through JavaScript/AJAX instead of the Browser actually navigating to them.

我想创建它,但作为一个跨平台桌面应用程序。我知道Atom Shell结合了Chromium-Browser和Node.js / v8运行时,但我不确定他们将如何沟通。

I want to create that, but as a Cross-Platform Desktop Application. I know that Atom Shell combines both a Chromium-Browser and a Node.js/v8 runtime, but I'm not sure how they would communicate.

我想我可以在Web服务器上运行完整(基本上,一些Node.js HTTP中间件,如 Express ),但这会创建一个网络可访问的服务器(也可能绊倒防火墙) - 我想制作桌面应用程序的原因之一就是避免运行真正的服务器。基本上像普通桌面应用程序中的MVP / MVVM模式。

I guess I could run a full on web server (basically, some Node.js HTTP Middleware like Express), but that creates a network-reachable server (which might also trip up firewalls) - one of the reasons I want to make a desktop app is precisely to avoid running a real server. Basically like the MVP/MVVM pattern in a "normal" desktop app.

有人可以给我一些起点,我正在尝试做什么吗?浏览器如何与节点运行时(他们称之为客户端?)对话告诉它嘿,用ID 12345获取我的记录并且客户端会返回呈现的HTML,或者浏览器是否会得到一个blob JSON回来并通过JavaScript模板引擎呈现它?

Can someone give me a few starting points for what I'm trying to do? How would the browser talk to the node runtime (the "Client" as they call it?) to tell it "Hey, fetch my the record with ID 12345" and would the Client return rendered HTML, or would the browser just get a blob of JSON back and render it through a JavaScript templating engine?

推荐答案

电子似乎没有将Node.js用作网络服务器,但仅作为运行后台JavaScript代码的环境,此代码可以使用节点模块来访问系统。与此同时,Chromium为应用程序提供了一个用户界面,它显示了运行常用沙盒JavaScript的常规网页。两者都被Electron可执行文件嵌入,前者直接嵌入(Node.js可以构建为静态库),后者通过 libchromiumcontent 。在某种程度上,Node.js是应用程序的控制器部分,而Chromium是视图。

Electron doesn't seem to use Node.js as a web server but merely as an environment to run background JavaScript code, this code can use node modules to access the system. At the same time Chromium provides a user interface for the app, it displays regular web pages that run usual sandboxed JavaScript. Both are being embedded by the Electron executable, the former directly (Node.js can be built as a static library), the latter via libchromiumcontent. In a way, Node.js is the controller part of the application whereas Chromium is the view.

通常,此处用于网页的概念是单页应用程序:网页代表一个应用程序窗口,因此只要此窗口可见,它就会保持不变(通常在应用程序的整个生命周期中)。每当它需要显示不同的东西时,它就会从Node.js中运行的后台代码中请求数据,就像AJAX应用程序从服务器请求数据一样。页面本身不会重新加载,通常会使用JavaScript模板来更新内容。

Typically, the concept used for web pages here is that of single-page applications: a web page represents one application window and as such it stays around as long as this window is visible (often for the entire lifetime of the application). Whenever it needs to display something different it requests data from the background code running in Node.js, just like AJAX applications request data from the server. The page itself is not reloaded, usually JavaScript templating will be used to update contents.

这里没有真正的服务器/客户端关系,但是通信实际上可以去双向。双方都可以使用 ipc 模块相互发送消息(主要流程渲染器)。这些消息可以附加任何参数,这些参数不需要显式编码(通常这是通过在内部使用JSON来编码参数来实现的,我没有验证Electron是否就是这种情况)。在内部,该消息传递是通过特定于平台的 IPC 机制实现的,使用libuv 准确无误。

There isn't really a server/client relationship here however, the communication can actually go both ways. Both sides can use the ipc module to send messages to each other (main process, renderer). These messages can have any arguments attached to them, these don't need to be encoded explicitly (typically this is implemented by using JSON internally to encode parameters, I didn't verify whether that's the case with Electron). Internally, that message passing is implemented via platform-specific IPC mechanisms, using libuv to be exact.

这篇关于使用Electron(Atom Shell)时的客户端/服务器模型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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