WSS works on http?

2020-07-09 06:39发布

问题:

It is possible having a wss protocol over http ? i've read on a forum that ws work with http, but wss works only with https ? Is that true ?

Cause i'm trying to test it on my wamp on localhost, but not working

回答1:

ws tells a WebSocket client library to use http to connect to a WebSocket server. Likewise, wss tells a WebSocket client library to use https to connect to a WebSocket server. Just that. "ws protocol" and "wss protocol" are strange words. "WebSocket protocol" is the right word. WebSocket protocol can be used over both plain HTTP connections (http) and secure HTTP connections (https).

Note that communication between a WebSocket client and a WebSocket server starts as a normal HTTP protocol. To start WebSocket communication, a WebSocket client sends a request like below to a WebSocket server (This is an excerpt from RFC 6455, 1.2. Protocol Overview).

GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Origin: http://example.com
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13

As you can see, this is a normal HTTP GET request. A WebSocket server can wait for this kind of requests on an unsecured port (http, 80 is the default) or on a secured port (https, 443 is the default). It's up to WebSocket servers.

If a WebSocket server you are using is waiting for requests on an unsecured port, pass ws to a WebSocket client library you are using. Otherwise, if the WebSocket server is waiting for requests on a secured port, pass wss to the WebSocket client library.

Some implementations of WebSocket client libraries accept not only ws and wss but also http and https just for developers' convenience.

"WSS on http" is a strange word. On the other hand, "WebSocket protocol on http" and "WebSocket protocol on https" make sense.