如何编写将在 Chrome 和 Node.js 中运行的 javascript Web 套接字代码?

发布于 2024-12-26 02:06:28 字数 2263 浏览 1 评论 0原文

这个问题的答案可以归结为两件事:1)考虑到 Node.js 的限制,使用 BOSH 或 Web 套接字会更简单吗?2)如何构建代码,以便同一个 JavaScript 文件在浏览器,就像 Chrome(或其他浏览器)中一样。很难简洁地描述这个问题。

真正的问题是希望代码在浏览器中像在 Node.js 中一样运行良好,同时具有外部依赖项。

背景

我的想法是我想做一些小型的 javascript 项目来娱乐。我把这个想法称为 jsFun,核心想法是我可以花 30 分钟到一个小时制作一些有趣的东西并与我的朋友分享。

我首先想看看我是否可以在一小时内编写俄罗斯方块,只使用 Notepad++、Chrome 和 Dropbox。我没有成功,但很有趣。

对我来说,“乐趣”可能意味着游戏,也可能意味着多人游戏。餐巾纸背面的草图如下所示:

  • 我可以从任何计算机进行更改并通过保管箱将其推送出去。 (检查!)
  • 我可以使用我的 Dropbox 公共 URL 来提供静态页面。 (检查!)
  • Web 客户端可以使用 HTML5 Web 套接字或 BOSH 通过 Node.js 聊天服务器路由消息。
  • 游戏服务器脚本还可以连接到聊天服务器并实现某种游戏逻辑。
  • 游戏服务器脚本可以在浏览器中运行,也可以在 Node.js 中运行。

以下是使用 HTML5 Web 套接字与 Node.js 聊天服务器通信的绝佳示例: http://html5demos.com/web-socket

假设我制作了多人井字游戏。我的项目需要 3 个部分:

  • 游戏客户端脚本 - 这是在浏览器中运行并为用户呈现游戏的 javascript。
  • 聊天中心脚本 - 这是在游戏客户端和游戏服务器之间传递消息的聊天服务器。它作为 Node.js 进程运行。
  • 游戏服务器脚本 - 该脚本可以在浏览器中运行以进行测试和调试,也可以在 Node.js 中运行。

现在,为了制作井字游戏,我将确保聊天服务器正在运行,创建一个游戏服务器脚本并游戏客户端脚本并打开三个 Web 浏览器 - 两个客户端和一个服务器。那时,我可以使用 Chrome 出色的调试工具来解决任何问题,在 notepad++ 中进行更新,并疯狂地刷新浏览器,持续 30 到 60 分钟。也许那时我有一个可以运行的游戏。

这是复杂的步骤: 我在浏览器中运行的游戏服务器脚本现在想从 node.js 运行。事实上,我想让聊天服务器监视我的 Dropbox 服务器脚本目录的更改,并自动运行这些脚本。

Node.js 使用 CommonJS 模块,浏览器无法加载这些模块。我认为我可以使用 RequireJS 并理论上在任一环境中加载代码,但问题是浏览器和服务器将使用不同的库来执行 Web 套接字 - 我如何制作以任一方式运行的代码? websockets 是否是一种可行的方法,因为它似乎是一个不断变化的标准,也许我不能依赖 node.js websocket 服务器来长期工作。

Node.js 唯一可用的 websockets 服务器看起来可能正在开发中: https://github.com/miksago/node-websocket-server

也许我应该使用像 BOSH 这样更成熟的 API?

此外,websocket 客户端也没有内置到 node.js 中,所以我必须使用它: (作为一个新的 stackoverflow 用户,我无法正常粘贴链接。它是 https://github.com/pgriess/node-websocket-client)

我必须面对我的游戏服务器代码的挑战,建立 webclient 连接对于聊天服务器,将在 Node.js 运行时环境中使用与 Chrome 浏览器环境中不同的库。

也许我可以不使用 require.js,而是在浏览器中使用标准 javascript 脚本并使用 node.js vm.runInContext - 看起来我可以在调用脚本之前使用类似的函数设置全局变量,并且它会工作得很好在 Node.js 或使用标准 JavaScript 代码的浏览器中以同样的方式进行。

重述问题

(假设我在这两种情况下都事先设置了全局环境,以便为脚本提供通用接口。)有没有一种实用的方法可以让我编写一个访问 websocket- 的 javascript 文件?类似客户端的功能,可以在浏览器或 Node.js 中执行?

The answer to this question boils down to two things: 1) Will it be simpler to use BOSH or web sockets, given the limitations of node.js, and 2) How can I structure code so the same javascript file runs equally well in the browser as it does in Chrome (or other browser). It is hard to describe the problem succinctly.

The real problem is wanting code to run equally well in the browser as in node.js, while having external dependencies.

Background

I got this idea that I want to do small javascript projects for fun. I'm calling the idea jsFun, and the core idea is that I can spend 30 minutes to an hour making something fun and sharing it with my friends.

I started off by seeing if I could write Tetris in an hour, using nothing but Notepad++, Chrome, and Dropbox. I didn't succeed, but it was fun.

For me, "fun" probably means a game, and it probably means multiplayer. The back-of-the-napkin sketch looks like this:

  • I can make my changes from any computer and push them out through dropbox. (check!)
  • I can use my dropbox public URL to serve static pages. (check!)
  • The web clients can use HTML5 web sockets or BOSH to route messages through a node.js chat server.
  • Game server scripts can also connect to the chat server and implement some sort of game logic.
  • Game server scripts can either be running in the browser or in node.js.

Here is an excellent example of using HTML5 web sockets talking to a node.js chat server:
http://html5demos.com/web-socket

Let's say I make multiplayer tic-tac-toe. My project needs 3 parts:

  • A game client script - this is the javascript that runs in the browser and renders the game for the user.
  • A chat hub script - this is a chat server that passes messages between the game clients and the game server. It runs as a node.js process.
  • A game server script - this script can run in the browser for testing and debugging, or in node.js

Now, to make tic-tac-toe, I'll make sure the chat server is running, create a the game server script and the game client script and open three web browsers - two clients and one server. At that point, I can use Chrome's awesome debugging tools to work through any problems, make my updates in notepad++, and refresh the browsers like crazy, for 30 to 60 minutes. And maybe I have a working game at that point.

This is the complicating step:
That game server script I was running in a browser, I now want to run from node.js. In fact, I want to have the chat server monitor my dropbox server script directory for changes, and automatically run those scripts.

Node.js uses CommonJS modules, which the browser can't load. I think I can use RequireJS and theoretically load the code in either environment, but then the problem becomes the fact that the browser and server will be using different libraries to do the web sockets -- how do I make code that runs either way? Is websockets even the way to go, since it seems like it is a standard in flux, and maybe I can't depend on the node.js websocket server to work long-term.

The only available websockets server for node.js looks like it may be a work in progress:
https://github.com/miksago/node-websocket-server

Maybe I should be using a more mature API like BOSH?

Additionally, the websocket client doesn't come built into node.js either, so I'd have to use this:
(As a new stackoverflow user, I can't paste the link normally. It's https:// github.com /pgriess/node-websocket-client)

I'd have to face the challenge that my game server code, making webclient connections to the chat server, will be using different libraries in the node.js runtime environment than in the Chrome browser environment.

And maybe instead of using require.js, I could use standard javascript scripts in the browser and use node.js vm.runInContext - it looks like I could set up the global variable with similar functions before calling the scripts and it would work pretty much the same way in node.js or the browser using standard javascript code.

Restating the Question

(Assuming I'd set up the global environment beforehand in either case to provide the script with a common interface.) Is there a practical way for me to write a javascript file that accesses websocket-client-like features, that can execute in the browser or in node.js?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宁愿没拥抱 2025-01-02 02:06:28

如果需要付出一些努力才能让网络套接字运行,请不要担心,因为回报的价值远远超过付出的努力。

在网络套接字出现之前,所有真正基于浏览器的产品都依赖于浏览器不断轮询服务器,这对于浏览器、网络和服务器来说都是沉重的负担。

即使是基于 HTTP 轮询的系统中的简单消息“hello”也可能导致需要处理数千字节的数据。

在 Web 套接字中,最坏情况下的开销量约为 15 个字节。

另一件事是网络套接字是事件驱动的,因此您的浏览器可以自由地保持空闲状态,或执行其他任务,直到发生实际的通信事件。

至于让代码在前端和后端运行良好的问题,这将是使您的 javascript 类模块化并在服务器端使用 require() 类型函数以及可能在客户端上模拟相同函数的问题侧面注入您创建的脚本。

有许多演示可供下载和修改,因此,就像任何新的编码领域一样,只需投入并亲自动手即可。很快就会明白了。

If it takes a bit of effort to get a web-sockets sockets running , fear not as the reward is worth far far far more than the effort.

Prior to web-sockets all truly browser based offerings have relied on the browser continuously polling the server, which is a heavy load for browser,network and server alike.

Even the simple message "hello" in a system based upon HTTP polling could result in kilobytes of data needing to be processed.

In web-sockets the amount of overhead at worst is something in the neighborhood of 15 bytes.

Another thing is that web-sockets are event driven, so your browser is free to remain idle, or doing other tasks until an actual communication event occurs.

As to your question of making code run well front end and back, it is going to be a matter of making your javascript classes modular and using the require() type functions on the server side and perhaps a simulation of that same function on the client side to inject the scripts you create.

There are many demos out there to download and tinker with , so like any new area of coding , just jump in and get your hands dirty. It will soon make sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文