我可以在浏览器中启动socket.io/websocket服务器吗? [英] Can I start a socket.io/websocket server in browser?

查看:76
本文介绍了我可以在浏览器中启动socket.io/websocket服务器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前对此有疑问,答案是否定的.但是现在,有了browserify/webpack,我可以像在服务器上那样编写代码,使其可以在浏览器中运行吗?或者是否存在任何限制使其无法实现?

There question about this before, the answer is no. But now, with browserify/webpack, can I just write code like I would on server and it will run in browser, or is there any restriction that would make this impossible?

推荐答案

否,您不能.在浏览器中启动服务器需要访问低级功能,而浏览器根本不具备该功能.Browserify无法向其没有的浏览器添加基本的低级功能,而这些功能需要附加的本机代码支持才能正常工作.

No, you cannot. Starting a server in a browser requires access to low level functionality that simply to does not exist in a browser. Browserify cannot add fundamental low-level features to the browser that it does not have that would require additional native code support in order to make work.

Browserify只能打包纯Javascript或在浏览器中已经存在的基础架构之上构建的代码,或者可以使用在浏览器中已存在的功能之上构建的某些纯JavaScript进行模拟的代码.

Browserify can only package code that is either pure Javascript or is built on top of infrastructure that already exists in the browser or can be simulated with some pure javascript built on top of the features that do exist in the browser.

例如,您可以从纯JavaScript的node.js中获取一个加密哈希库,并且不依赖浏览器中不存在的任何功能,并且可以对其进行浏览器化(例如重新打包)以使用它在浏览器中.但是,您不能采用使用低级UDP通信的node.js程序包,因为浏览器中不存在对UDP的基本访问.

So, for example, you could take a crypto hash library from node.js that is pure javascript and does not rely on any capabilities that are not present in a browser and you could browserify it (e.g. repackage it) to use it in a browser. But, you could not take a node.js package that uses low-level UDP communication because the underlying access to UDP is not present in a browser.

通常,如果node.js代码执行I/O或操纵其他进程或使用具有本机代码的任何模块,则它可能不适用于browserify(某些文件I/O有一些解决方法).

In general, if the node.js code does I/O or manipulates other processes or uses any module that has native code, it will likely not work with browserify (there are a few work-arounds with some file I/O).

有关其他信息,请参见:

For additional info, see:

Browserify是否有任何限制?

浏览器兼容性

因此,您不必说出您要解决的实际问题是什么,但是通常您会在某个地方启动实际的服务器,并使浏览器连接到该服务器.如果您希望某个特定的浏览器会话看起来像是主"会话,则可以使您的客户端/服务器以这种方式运行.一个客户端可以是连接到同一服务器的其他客户端的主服务器(似乎本质上是服务器本身).所有这些都将通过您如何对服务器进行编程以及如何与连接到该服务器的各种客户端进行通信来完成.可以将实际服务器用作其他客户端的代理,在该客户端中,客户端可以通过与实际服务器的代理连接来访问类似服务器的功能.

So you don't say what your actual problem is that you're trying to solve, but usually you would start an actual server somewhere and have the browser connect to that server. If you wanted one particular browser session to appear to be the "master", you could certainly make your client/server behave that way. One client could be the master (appearing to essentially be the server itself) to other clients that connected to that same server. This would all be done by how you programmed your server and how it communicated with the various clients that connect to it. Actual servers can be made to be proxies for other clients where the client gets access to server-like functionality via the proxied connection to an actual server.

这篇关于我可以在浏览器中启动socket.io/websocket服务器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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