html5 Websocket with SSL

2019-01-04 00:01发布

Is it possible to have WebSockets with https? When switching to https, my WebSocket returns a security error and works perfectly with regular http...

socket = new WebSocket("ws://my_www:1235"); 

Thank you

3条回答
做自己的国王
2楼-- · 2019-01-04 00:40

The WebSocket connection starts its life with an HTTP or HTTPS handshake. When the page is accessed through HTTP, you can use WS or WSS (WebSocket secure: WS over TLS) . However, when your page is loaded through HTTPS, you can only use WSS - browsers don't allow to "downgrade" security.

查看更多
Bombasti
3楼-- · 2019-01-04 00:44

You can't use WebSockets over HTTPS, but you can use WebSockets over TLS (HTTPS is HTTP over TLS). Just use "wss://" in the URI.

I believe recent version of Firefox won't let you use non-TLS WebSockets from an HTTPS page, but the reverse shouldn't be a problem.

查看更多
欢心
4楼-- · 2019-01-04 01:00

1 additional caveat (besides the answer by kanaka/peter): if you use WSS, and the server certificate is not acceptable to the browser, you may not get any browser rendered dialog (like it happens for Web pages). This is because WebSockets is treated as a so-called "subresource", and certificate accept / security exception / whatever dialogs are not rendered for subresources.

查看更多
登录 后发表回答