websocket – ws和wss之间的区别?

将ws变成wss的程序是什么?

wss是通过普通HTTP进行升级还是wss只能用于HTTPS?

webSocket = new WebSocket("ws://localhost:port/Esv/ocp");

当我把ws改为wss时工作正常

webSocket = new WebSocket("wss://localhost:port/Esv/ocp");

它显示了这个错误:

Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR

最佳答案
简洁版本

SSL或SSL

您可能有一个SSL certificate issue.连接点规则可以是summarized as

> wss仅在https上连接
> ws连接http

反之亦然:

> https仅接受wss
> http仅接受ws

错误

以下情况将导致您出错(在Firefox下完成测试):

>如果要将wss连接连接到http端点.在我的测试中,我有一个

InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable

>如果要将ws连接连接到https端点,则会出现错误

SecurityError: The operation is insecure.

正式答案

websocket的圣经是RFC 6455.在第4.1.5节:

If /secure/ is true, the client MUST perform a TLS handshake over the connection after opening the connection and before sending the handshake data [RFC2818]. If this fails (e.g., the server’s certificate could not be verified), then the client MUST Fail the WebSocket Connection and abort the connection. Otherwise, all further communication on this channel MUST run through the encrypted tunnel [RFC5246].

安全标志由URI定义.第3节定义了什么是安全的

The URI is called “secure” (and it is said that “the secure flag is set”) if the scheme component matches “wss” case-insensitively.

TL; DR

如果你想使用wss:

>您必须激活SSL
>必须保护您的端点(https:// …):不允许“安全降级”

如果你想使用ws:

>确保您的端点未启用SSL(http:// …)

转载注明原文:websocket – ws和wss之间的区别? - 代码日志