在我的命名空间中使用 socket.io

发布于 2025-01-05 01:27:49 字数 150 浏览 1 评论 0原文

我将 socket.io 作为代码的一部分提供给第三方网站使用。我不希望它污染全局命名空间(例如,不同版本的 io 会发生冲突),而是让它仅作为我的库的一部分工作,因此它将被命名空间并仅从

MyLibrary.io

我该怎么办?

I'm shipping socket.io as part of my code for 3rd party sites to use. I don't want it to pollute the global namespace (e.g. different versions of io will collide) but rather make it work only as part of my library, So it will be namespaced and called only from

MyLibrary.io

How do I go about this?

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

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

发布评论

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

评论(2

很酷又爱笑 2025-01-12 01:27:49

由于您可以完全控制该文件,因此最简单的方法就是修改该文件以在新位置加载 socket.io。

我想说的最简单的方法是将 dist/socket.io.js 的内容包装起来,

(function() {
  // All Standard SocketIO code

}).call(MyLibrary);

这将使文件将 io 加载到 MyLibrary 上 相反。 SocketIO 与许多库一样,使用 this 来决定如何加载内容。在浏览器环境中,this 是对象 window,但是通过将函数包装在所有内容上,您可以控制 this 并将其更改为您自己的使用call方法来获取值。

许多其他库(尽管不是 socketIO)都有一个帮助器来避免这个问题。他们通常有一个名为 noConflict() 的方法。例如,如果您想避免 jQuery 出现这样的问题,您可以这样做:

MyLibrary.$ = MyLibrary.jQuery = jQuery.noConflict(true);

这是可行的,因为当 jQuery 加载时,它会保存对先前加载版本的引用,以便在需要时可以将全局对象设置回来他们是怎样的。

Since you have total control over the file, then the easiest thing to do would be to just modify the file to load socket.io at a new location.

The easiest way I would say to do that would be to wrap the contents of dist/socket.io.js with

(function() {
  // All Standard SocketIO code

}).call(MyLibrary);

That will make the file load io onto MyLibrary instead. SocketIO, like many libraries, uses this to decide how to load things. In a browser circumstance, this is the object window, but by wrapping a function around everything, you can control this and change it to your own value by using the method call.

Many other libraries, though not socketIO, have a helper to avoid exactly this problem. Often they have a method called noConflict(). For example, if you wanted to avoid a problem like this for jQuery, you could do:

MyLibrary.$ = MyLibrary.jQuery = jQuery.noConflict(true);

That works because when jQuery loads, it saves a reference to the previously loaded version, so that if it needs to, it can set the global objects back to how they were.

厌味 2025-01-12 01:27:49

似乎建议的方法(Issue #85)是按照正常方式加载它,然后运行这两行:

MyLibrary.io = window.io; // Assign it to your desired namespace
delete window.io; // Remove it from the window namespace

socket.io.js 中的代码 onlyio 变量添加到 >窗口

您可以修改 socket.io.js 中的代码,但这可能会使将来的维护变得困难,因为您必须跟踪对该文件所做的更改并将其传播到较新版本的 socket.io.js

起初,我认为你可以用 module.exports 做一些事情,因为它有 CommonJS 格式,但是,在阅读代码后,我意识到这只是构建 socket.io.js 文件之前组织事物的方式。请参阅此处的文件目录了解我所指的内容。

It seems that the suggested way to do this (Issue #85) is to load it as per normal then run these two lines:

MyLibrary.io = window.io; // Assign it to your desired namespace
delete window.io; // Remove it from the window namespace

The code in socket.io.js only adds the io variable to the window.

You could modify the code in socket.io.js but that might make it hard to maintain in the future since you would have to track changes you make to that file and propagate them into newer versions of socket.io.js.

At first, I thought you could do something with module.exports since it had the CommonJS format, however, after reading the code, I realized it's just the way things were organized before building the socket.io.js file. See the file directory here for what I'm referring to.

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