gpt4 book ai didi

javascript - Socket I - 它如何自动连接回服务器?

转载 作者:行者123 更新时间:2023-11-27 22:34:37 24 4
gpt4 key购买 nike

正如本文中提到的tutorial使用 Socket.IO 创建聊天应用程序:

Notice that I’m not specifying any URL when I call io(), since it defaults to trying to connect to the host that serves the page.

我想知道它是怎么做到的?如何通过客户端的 JavaScript 检索为该页面提供服务的服务器的详细信息?我尝试搜索 Socket.IO ,但无法找到 io() 函数。

有人可以指出检索这些元详细信息的代码或显示具有相同功能的小片段吗?

最佳答案

在浏览器 JavaScript 中,window.location 对象具有以下相关属性:

window.location.host    - The hostname of the current webpage
window.location.port - The port number of the current web page

其他属性在这里:https://developer.mozilla.org/en-US/docs/Web/API/Location

因此,socket.io 可以使用这两个值来连接回当前网页所在的主机。您可以在 the source 中的 socket.io 客户端文件中看到一些这样的逻辑。 .

关于javascript - Socket I - 它如何自动连接回服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39240907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com