[RRQMSocket.WebSocket] C# build WebSocket server and client

1, Overview

1.1 what is WebSocket??

WebSocket is a protocol for full duplex communication over a single TCP connection. WebSocket communication protocol was defined as standard RFC 6455 by IETF in 2011 and supplemented by RFC7936. The WebSocket API is also standardized by W3C.
WebSocket makes the data exchange between the client and the server easier, and allows the server to actively push data to the client. In the WebSocket API, the browser and server only need to complete a handshake, and they can directly create a persistent connection and conduct two-way data transmission.

1.2 RRQMSocket.WebSocket feature

  1. The performance is excellent. Compared with similar products, the performance is improved by about 20%, thanks to the powerful RRQMSocket.
  2. Elegant encapsulation, inheriting encapsulation from TCP, can send big data without subcontracting and small data in pieces.
  3. The analysis is simple. After receiving the message, the data frame has been converted into a data object. The data type can be judged by enumeration value, and FIN, RSV1-3 and other data can be judged by Boolean value.
  4. It can send binary data or text directly.

1.3 detailed explanation of config configuration

[RRQM system] Config configuration and other configuration information

2, Assembly source code, Demo download

2.1 source location

RRQMSocket

2.2 Demo location

RRQMBox

3, Installation

Install rrqmsocket Just use websocket. See the link blog for specific steps.

VS, Unity installation and use of Nuget package

4, Create WebSocket server

The creation of the server is consistent with the TCP system. It can be inherited from WSService and then WSSocketClient, or it can be instantiated directly from SimpleWSService. The following code is from the SimpleWSService example.

SimpleWSService wSService = new SimpleWSService();
wSService.Connected += (client,e) =>
{
    //The client completes the connection
};

wSService.Received += (client, dataFrame) =>
{
    switch (dataFrame.Opcode)
    {
        case WSDataType.Cont:
            Console.WriteLine($"Intermediate data received, length:{dataFrame.PayloadLength}");
            break;
        case WSDataType.Text:
            Console.WriteLine(dataFrame.GetMessage());
            break;
        case WSDataType.Binary:
            if (dataFrame.FIN)
            {
                Console.WriteLine($"Binary data received, length:{dataFrame.PayloadLength}");
            }
            else
            {
                Console.WriteLine($"Received unfinished binary data, length:{dataFrame.PayloadLength}");
            }
            break;
        case WSDataType.Close:
            break;
        case WSDataType.Ping:
            break;
        case WSDataType.Pong:
            break;
        default:
            break;
    }

    client.Send(dataFrame.PayloadData);//Send back the received data
};
wSService.Setup(7789).Start();//Here is a simple configuration. For detailed configuration, see Config configuration

5, Create WebSocket client

The creation of the client is consistent with that of TcpClient. It can be inherited from WSClient or instantiated directly from SimpleWSClient. The following code is from the SimpleWSClient example.

SimpleWSClient simpleWSClient = new SimpleWSClient();
simpleWSClient.Setup("127.0.0.1:7789").Connect();
simpleWSClient.Received += (client, dataFrame) =>
            {
                switch (dataFrame.Opcode)
                {
                    case WSDataType.Cont:
                        Console.WriteLine($"Intermediate data received, length:{dataFrame.PayloadLength}");
                        break;
                    case WSDataType.Text:
                        Console.WriteLine(dataFrame.GetMessage());
                        break;
                    case WSDataType.Binary:
                        if (dataFrame.FIN)
                        {
                            Console.WriteLine($"Binary data received, length:{dataFrame.PayloadLength}");
                        }
                        else
                        {
                            Console.WriteLine($"Received unfinished binary data, length:{dataFrame.PayloadLength}");
                        }
                        break;
                    case WSDataType.Close:
                        break;
                    case WSDataType.Ping:
                        break;
                    case WSDataType.Pong:
                        break;
                    default:
                        break;
                }
            };
while (true)
{
    simpleWSClient.Send(Console.ReadLine());//Send text
}

6, Send data

6.1 direct transmission

In IWSClientBase (the client is implemented as WSClient and the server is implemented as WSSocketClient), the Send function has been rewritten. If Send is called directly, the data will be automatically encapsulated as a binary packet.

6.2 slice sending

WebSocket supports fragment sending, which is designed to solve the transmission of big data. Because components with insufficient receiving cache such as browsers cannot receive one big data packet at a time, they must be fragment. However, in RRQM, the function of automatic fragmentation is not designed, but the sender is allowed to decide whether to fragment or not and how large each packet should be.

The sliced function has also been encapsulated. Taking the first function as an example, the first three parameters are positioning and sending data, and the last data determines the size of each data packet.

Take the following code as an example, the data will be sent from the data of indexes 1-8, and the maximum length of each data packet is 4, just two packets will be sent.

byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
simpleWSClient.SubpackageSend(data,1,8,4);

7, Shortcomings and follow-up plans

Disadvantages:

  1. ssl is not supported at the moment.
  2. Agent is not supported at the moment.

Follow up plan:

  1. overcome a fault
  2. Provide file transfer function.
  3. Provides RPC functionality.

Keywords: C# server websocket

Added by sloshire1 on Tue, 28 Dec 2021 01:51:00 +0200