简体   繁体   English

如何在socket.io上本地测试https?

[英]How do I test https on socket.io locally?

I've implemented an application using socket.io. 我已经使用socket.io实现了一个应用程序。 All working nicely. 一切都很好。 Converting it to a progressive web app required SSL so I've done that and that's working nicely. 将其转换为渐进式Web应用程序需要SSL,因此我做到了,而且效果很好。 The app installs as a web app and works. 该应用程序将作为网络应用程序安装并运行。

My problem is that when I go back to work on new features, the local installation of the app server ignores my secure client requests (On the production system, nginx handles the SSL). 我的问题是,当我重新使用新功能时,应用服务器的本地安装会忽略我的安全客户端请求(在生产系统上,nginx处理SSL)。

These are the offending lines in the client: 这些是客户端中令人反感的行:

const socket_url = 'https://' + url.hostname + ':' + port;
const socket = openSocket(socket_url, {query: 'clientId=' + clientId, secure: true});

I could edit the relevant line in the client conditionally on production versus local development but I'm hoping there is a more elegant solution. 可以根据生产条件还是本地开发条件在客户中编辑相关的生产线,但是我希望有一个更优雅的解决方案。

A couple ideas for dealing with the test environment vs. production environment issue: 处理测试环境与生产环境问题的一些想法:

  1. You could get a self-signed certificate and use it locally on your https server and have your test browser trust it. 您可以获取自签名证书,并在https服务器上本地使用它,并使您的测试浏览器信任它。 Then, you run https locally. 然后,您在本地运行https。 You will, of course, have to adapt your server code to be an https server also when run in the test environment. 当然,在测试环境中运行时,您也必须将服务器代码修改为https服务器。
  2. You could set up your own NGINX locally with a self-signed certificate and have it provide the same https role that you have in production. 您可以使用自签名证书在本地设置自己的NGINX,并使它提供与生产中相同的https角色。
  3. When running locally, your server could generate a slightly different web page that inserts http:// instead of https:// in the code you show in your question so the client would just automatically use the desired protocol (https in production and http in test). 在本地运行时,您的服务器可能会生成一个稍微不同的网页,该网页会在问题中显示的代码中插入http://而不是https:// ,因此客户端将自动使用所需的协议(生产环境中使用https,测试)。 This could be done with a template engine or could be done manually with search/replace when serving the relevant script. 这可以通过模板引擎完成,也可以在提供相关脚本时通过搜索/替换手动完成。
  4. The client-side Javascript could use location.protocol instead of a hard code https which will then automatically use the protocol that the web page was loaded from which could then be either http or https. 客户端Javascript可以使用location.protocol代替硬代码https ,然后将自动使用从中加载网页的协议,该协议可以是http或https。 So, if the web page is loaded via http, then socket.io would use http. 因此,如果网页是通过http加载的,则socket.io将使用http。 If the web page was loaded via https, then socket.io would use https. 如果网页是通过https加载的,则socket.io将使用https。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM