Swift-based WebSocket framework :Starscream, using Node.js to build the background and realize the Demo of WebSocket communication. Project address: SSRWebSocket

Start the

  1. npm install ws

  2. Node websocket.js Enables the Websocket service

  3. Run the APP(Run will automatically open a client.html) to chat

concept

WebSocket is based on TCP protocol and full-duplex communication technology, which enables the server and client to communicate with each other.

Features:

  1. Based on TCP transport protocol, reuse HTTP handshake channel
  2. With HTTP protocol has good compatibility, the default port is 80(ws:) and 443(WSS :), and the handshake phase uses HTTP protocol, because the handshake is not easy to shield, can pass a variety of HTTP proxy server.
  3. The data format is relatively light, with low performance overhead and high communication efficiency
  4. You can send text, you can send binary files,
  5. Both parties can voluntarily close the link
  6. The protocol id iswsorWSS (encryption), such asws://localhost:80

Limitations of WebSocket

  1. WebSocketAchieving scalability is more difficult in HTTP communication systems with load balancing. WebSocket only makes one HTTP connection
  2. WebSocketLong – connected heartbeats are more complex to maintainWebSocketThe long connection is based onPing/PongTo maintain the heartbeat mechanism.

WebSocket communication process

  1. First, the client initiates an HTTP request and adds an Upgrade: webSocket field in the request header Heade, indicating that the protocol is to be upgraded to WebSocket.

    Client request message and implementation client request message:

    GET/HTTP/1.1 Upgrade: websocket Connection: Upgrade Host: example.com Origin: http://example.com sec-websocket-key: sN9cRrP/n9NdMgdcy2VJFQ== Sec-WebSocket-Version: 13Copy the code

    Different from traditional packets:

    Upgrade: websocket
    Connection: Upgrade
    Copy the code

    The next two lines represent information about the Webscoket protocol

    Sec-websocket-key: sN9cRrP/n9NdMgdcy2VJFQ== // Generate sec-websocket-version: 13 randomlyCopy the code
  2. Upon receipt, the server responds with a handshake acknowledgement, sending 101 switching Protocols that allow the client to use the WebSocket protocol.

    The server responds to the packet

    HTTP/1.1 101 Switching Protocols
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
    Sec-WebSocket-Protocol: chat
    Copy the code
  3. The two ends can now send messages to each other.

    WebSocket requires only one HTTP handshake

Four life cycles of WebSocket endpoints

  • Open event: This event occurs before the endpoint establishes a new connection and before any other events occur.

  • Message Event: This event receives a message sent by the other end of a WebSocket conversation. This can happen at any time after the WebSocket endpoint receives an open event and before the WebSocket endpoint receives a close event to close the connection.

  • Error event: This event is generated when a WebSocket connection or endpoint has an error.

  • Closed event: This event indicates that the WebSocket endpoint’s connection is currently partially closed and can be emitted by any of the participating connections.

FAQ?

  1. The Socket dead links

    A Socket dead link is a waste of resources when one party is closed and the other party is waiting for a Socket connection.

    The client sends heartbeat commands to the server within a fixed period of time to let the server know that the client is alive.

  2. What is the meaning of WebSocket Ping and Pong?

    In the long connection mechanism, the client tells the server that Ping is alive, and the server tells the client that Pong is alive

Reference

Swift version of the WebSocket framework Starscream

Node.js implementation of WebSocket chat room example