What is the difference between WebSocket and HTTP?

  • Webscoket is a bidirectional communication protocol that simulates the Scoket protocol and can send or receive messages bidirectionally. HTTP is one-way.
  • Websocket requires a handshake between the browser and the server to establish a connection, whereas HTTP requires the browser to send a request to the server, which does not know about the connection in advance.
  • Webscoket has no same-origin restriction, and clients can communicate with any server.

What is the difference between a WebSocket and a scoket?

A Scoket is a transport control layer interface that encapsulates the transport layer protocol (mainly known as TCP/IP) at the program level. We know that the protocol of the transport layer is to solve the transmission of data in the network, so the socket is the interface at both ends of the transmission channel. So for the front end, socket can also be simply understood as the abstract protocol of TCP/IP.

Webscoket is an application-layer protocol that is wrapped as an application-layer protocol as a socket, allowing clients and remote servers to establish full-duplex communication over the Web. Websocket provides BOTH WS and WSS URL schemes.

3. Can HTTP and WebSocket share a port? Why?

Websocket is the upgrade version of HTTP. We only need to customize the upgrade function of the HTTP server. Let the upgrade function act as a router for the WebSocket (multiple Websockets sharing the same port can be forwarded through the router).

4, WebSocket how to do heartbeat detection, data encryption, authentication?

5. What protocol is used to transmit the WebSocket handshake? What status is returned after a successful handshake? 200?

The WS handshake phase uses the HTTP protocol. The readyState property returns the current state of the instance object. There are four types:

  • CONNECTING: The value is 0, indicating that a connection is being established.
  • OPEN: the value is 1, indicating that the connection is successful and communication can be started.
  • CLOSING: A value of 2 indicates that the connection is CLOSING.
  • CLOSED: the value is 3, indicating that the connection is CLOSED or fails to be opened.

The returned status code is 101. For details, see developer.mozilla.org/zh-CN/docs/…

6. Can websockets carry cookies? Why? And if so, how?

Websockets are TCP/IP layer links, while cookies are application layer HTTP protocol things, so can’t. Unless websocket is used in cases where the browser does not support the degradation scheme. And established a socket connection, as long as the continuous open, both sides can send data to each other. If I want a cookie, the server just tells the client I need a cookie, and the client sends a document. Cookie, and the server gets it. Cookies and sessions were created to address HTTP as a stateless protocol. The socket is always connected and there is no need for cookies. To think of it another way, cookies can only hold 64K of content. HTML5 native storage can hold 1-5m of content. If the server needs it, it can also send the content stored locally to the server through the socket to play more roles.

7. How does WebSocket distinguish between different clients?

There are two options: one is to get the current user by fetching the value in the HttpSession on the Server, and the other is to attach the value of the user directly when the client establishes the connection. Reference: blog.csdn.net/qq_33171970…

What’s the difference between WebSocket and HTTP2? Can HTTP2 replace WebSocket? why



Cloud.tencent.com/developer/a…