【发布时间】:2022-11-17 14:20:38
【问题描述】:
我有一个工作的基于 node.js express 的服务器(和客户端)应用程序here,它通过 http+websockets 显示 RPC。这在本地运行(使用 devcontainers)时非常有效,包括 Dockerfile 和 devcontainer.json。但是,当从代码空间运行时,它会失败并显示以下客户端错误消息。
client.js:9 Mixed Content:
The page at 'https://aniongithub-jsonrpc-bidirectional-example-<redacted>-8080.preview.app.github.dev/'
was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint
'ws://aniongithub-jsonrpc-bidirectional-example-<redacted>-8080.preview.app.github.dev/api'.
This request has been blocked; this endpoint must be available over WSS.
(anonymous) @ client.js:9
client.js:9 Uncaught DOMException: Failed to construct 'WebSocket':
An insecure WebSocket connection may not be initiated from a page loaded over HTTPS
at 'https://aniongithub-jsonrpc-bidirectional-example-<redacted>-8080.preview.app.github.dev/client.js:9:10'
文档here指出By default, GitHub Codespaces forwards ports using HTTP but you can update any port to use HTTPS, as needed。当我检查指示的设置时:
它设置为http。我在这里错过了什么?我怎样才能让它通过http 为我的express 应用程序提供服务?
笔记:我的意图是,当在本地克隆并在 devcontainer 中打开时,代码的工作方式与在 CodeSpace 中打开时一样。这意味着我需要确保将 CodeSpaces 生成的证书以某种方式纳入我的本地 devcontainer 进程,或者我完全放弃身份验证。或者,我需要查明我是否在 CodeSpaces 上运行并做不同的事情,这看起来很混乱,但不应该是这样。希望这能让我问这个问题的意图更清楚!
【问题讨论】:
-
您的本地地址是 https 但它正在尝试通过 http 连接到 WS,您需要更改“ws://aniongithub-jsonrpc-bidirectional-example-<redacted>-8080.preview.app.github.dev/api " 到 wss://your_socket_url
-
@Aditya_Anand 可能适用于 CodeSpaces 版本,但我的本地 devcontainer 版本不会。我试图找到一个解决方案,其中本地 devcontainer 版本和 CodeSpaces 版本都“正常工作”,这似乎是使用 http(并且文档表明应该是可能的)。编辑我的问题并添加此说明。
-
我相信可以使用您在不同环境中使用的 .env 文件来处理,可以在 .env 文件中定义协议(http 或 https)。然后您的 docker compose 可以根据部署环境使用它。
-
@Aditya_Anand 我不确定你是否理解,devcontainer 是由 VSCode 基于
devcontainer.json文件和那里指定的 Dockerfile(或docker-compose.yml)创建的。不幸的是,由于我没有部署它——我要求 Github 在 Codespace 中为我打开一个 repo,这不是我可以传递参数的东西。事实上,这就是我的问题——如何检测它们是否在 Github CodeSpace 中运行?
标签: node.js github vscode-devcontainer github-codespaces