用 C# 编写 WebSocket 服务器

如果您想使用 WebSocket API,拥有一个服务器会很有帮助。在本文中,我将向您展示如何用 C# 编写一个。您可以使用任何服务器端语言来实现,但为了保持简单和易于理解,我选择了微软的语言。

此服务器符合 RFC 6455,因此它只能处理来自 Chrome 16、Firefox 11、IE 10 及更高版本的连接。

第一步

WebSocket 通过 TCP(传输控制协议) 连接进行通信。幸运的是,C# 有一个 TcpListener 类,它的功能正如其名。它位于 System.Net.Sockets 命名空间中。

注意: 使用 using 关键字包含命名空间是一个好主意,这样可以减少输入量。它允许您在不每次都输入完整命名空间的情况下使用命名空间中的类。

TcpListener

构造函数

cs
TcpListener(System.Net.IPAddress localAddr, int port)

localAddr 指定监听器的 IP,port 指定端口。

注意: 要从 string 创建 IPAddress 对象,请使用 IPAddressParse 静态方法。

方法

  • Start()
  • System.Net.Sockets.TcpClient AcceptTcpClient() 等待 TCP 连接,接受它并将其作为 TcpClient 对象返回。

这是一个最基本的服务器实现

cs
using System.Net.Sockets;
using System.Net;
using System;

class Server {
    public static void Main() {
        TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 80);

        server.Start();
        Console.WriteLine("Server has started on 127.0.0.1:80.{0}Waiting for a connection…", Environment.NewLine);

        TcpClient client = server.AcceptTcpClient();

        Console.WriteLine("A client connected.");
    }
}

TcpClient

方法

  • System.Net.Sockets.NetworkStream GetStream() 获取流,这是通信通道。通道的两端都具有读取和写入功能。

属性

  • int Available 此属性指示已发送多少字节的数据。在 NetworkStream.DataAvailabletrue 之前,该值为零。

NetworkStream

方法

  • 从缓冲区写入字节,offset 和 size 确定消息的长度。

    cs
    Write(byte[] buffer, int offset, int size)
    
  • 将字节读入 bufferoffsetsize 确定消息的长度。

    cs
    Read(byte[] buffer, int offset, int size)
    

让我们扩展我们的示例。

cs
TcpClient client = server.AcceptTcpClient();

Console.WriteLine("A client connected.");

NetworkStream stream = client.GetStream();

// Enter to an infinite cycle to be able to handle every change in stream
while (true) {
    while (!stream.DataAvailable);

    byte[] bytes = new byte[client.Available];

    stream.Read(bytes, 0, bytes.Length);
}

握手

当客户端连接到服务器时,它会发送一个 GET 请求,将连接从一个简单的 HTTP 请求升级为 WebSocket。这被称为握手。

此示例代码可以检测到客户端的 GET 请求。请注意,这将阻塞直到消息的前 3 个字节可用。在生产环境中应研究替代解决方案。

cs
using System.Text;
using System.Text.RegularExpressions;

while(client.Available < 3)
{
   // wait for enough bytes to be available
}

byte[] bytes = new byte[client.Available];

stream.Read(bytes, 0, bytes.Length);

// Translate bytes of request to string
String data = Encoding.UTF8.GetString(bytes);

if (Regex.IsMatch(data, "^GET")) {

} else {

}

响应很容易构建,但可能有点难以理解。服务器握手的完整解释可以在 RFC 6455 的第 4.2.2 节中找到。就我们而言,我们将构建一个简单的响应。

您必须

  1. 获取“Sec-WebSocket-Key”请求头的值,不带任何前导或尾随空格
  2. 将其与“258EAFA5-E914-47DA-95CA-C5AB0DC85B11”连接(RFC 6455 指定的特殊 GUID)
  3. 计算新值的 SHA-1 和 Base64 散列
  4. 将散列写回作为 HTTP 响应中 Sec-WebSocket-Accept 响应头的值
cs
if (new System.Text.RegularExpressions.Regex("^GET").IsMatch(data))
{
    const string eol = "\r\n"; // HTTP/1.1 defines the sequence CR LF as the end-of-line marker

    byte[] response = Encoding.UTF8.GetBytes("HTTP/1.1 101 Switching Protocols" + eol
        + "Connection: Upgrade" + eol
        + "Upgrade: websocket" + eol
        + "Sec-WebSocket-Accept: " + Convert.ToBase64String(
            System.Security.Cryptography.SHA1.Create().ComputeHash(
                Encoding.UTF8.GetBytes(
                    new System.Text.RegularExpressions.Regex("Sec-WebSocket-Key: (.*)").Match(data).Groups[1].Value.Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
                )
            )
        ) + eol
        + eol);

    stream.Write(response, 0, response.Length);
}

解码消息

成功握手后,客户端将向服务器发送编码的消息。

如果我们发送“MDN”,我们会得到这些字节

129 131 61 84 35 6 112 16 109

让我们看看这些字节的含义。

第一个字节,当前值为 129,是一个位字段,分解如下

FIN (位 0) RSV1 (位 1) RSV2 (位 2) RSV3 (位 3) Opcode (位 4:7)
1 0 0 0 0x1=0001
  • FIN 位:此位指示是否已从客户端发送完整消息。消息可以分帧发送,但目前我们将保持简单。
  • RSV1、RSV2、RSV3:除非协商了提供非零值的扩展,否则这些位必须为 0。
  • Opcode:这些位描述了接收到的消息类型。Opcode 0x1 表示这是一个文本消息。Opcode 完整列表

第二个字节,当前值为 131,是另一个位字段,分解如下

MASK (位 0) Payload Length (位 1:7)
1 0x83=0000011
  • MASK 位:定义“Payload data”是否被掩码。如果设置为 1,则 Masking-Key 中存在一个掩码密钥,用于解除“Payload data”的掩码。所有从客户端到服务器的消息都有此位设置为 1。
  • Payload Length:如果此值在 0 到 125 之间,则它是消息的长度。如果为 126,则接下来的 2 个字节(16 位无符号整数)是长度。如果为 127,则接下来的 8 个字节(64 位无符号整数)是长度。

注意: 因为客户端到服务器的消息的第一位总是 1,所以您可以从该字节中减去 128 来去除 MASK 位。

请注意,我们的消息中设置了 MASK 位。这意味着接下来的四个字节(61、84、35 和 6)是用于解码消息的掩码字节。这些字节会随着每条消息而变化。

剩余的字节是编码的消息负载。

解码算法

D_i = E_i XOR M_(i mod 4)

其中 D 是解码后的消息数组,E 是编码后的消息数组,M 是掩码字节数组,i 是要解码的消息字节的索引。

C# 中的示例

cs
byte[] decoded = new byte[3];
byte[] encoded = new byte[3] {112, 16, 109};
byte[] mask = new byte[4] {61, 84, 35, 6};

for (int i = 0; i < encoded.Length; i++) {
    decoded[i] = (byte)(encoded[i] ^ mask[i % 4]);
}

整合

ws-server.cs

cs
//
// csc ws-server.cs
// ws-server.exe

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;

class Server {
    public static void Main() {
        string ip = "127.0.0.1";
        int port = 80;
        var server = new TcpListener(IPAddress.Parse(ip), port);

        server.Start();
        Console.WriteLine("Server has started on {0}:{1}, Waiting for a connection…", ip, port);

        TcpClient client = server.AcceptTcpClient();
        Console.WriteLine("A client connected.");

        NetworkStream stream = client.GetStream();

        // enter to an infinite cycle to be able to handle every change in stream
        while (true) {
            while (!stream.DataAvailable);
            while (client.Available < 3); // match against "get"

            byte[] bytes = new byte[client.Available];
            stream.Read(bytes, 0, bytes.Length);
            string s = Encoding.UTF8.GetString(bytes);

            if (Regex.IsMatch(s, "^GET", RegexOptions.IgnoreCase)) {
                Console.WriteLine("=====Handshaking from client=====\n{0}", s);

                // 1. Obtain the value of the "Sec-WebSocket-Key" request header without any leading or trailing whitespace
                // 2. Concatenate it with "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" (a special GUID specified by RFC 6455)
                // 3. Compute SHA-1 and Base64 hash of the new value
                // 4. Write the hash back as the value of "Sec-WebSocket-Accept" response header in an HTTP response
                string swk = Regex.Match(s, "Sec-WebSocket-Key: (.*)").Groups[1].Value.Trim();
                string swkAndSalt = swk + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
                byte[] swkAndSaltSha1 = System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(swkAndSalt));
                string swkAndSaltSha1Base64 = Convert.ToBase64String(swkAndSaltSha1);

                // HTTP/1.1 defines the sequence CR LF as the end-of-line marker
                byte[] response = Encoding.UTF8.GetBytes(
                    "HTTP/1.1 101 Switching Protocols\r\n" +
                    "Connection: Upgrade\r\n" +
                    "Upgrade: websocket\r\n" +
                    "Sec-WebSocket-Accept: " + swkAndSaltSha1Base64 + "\r\n\r\n");

                stream.Write(response, 0, response.Length);
            } else {
                bool fin = (bytes[0] & 0b10000000) != 0,
                    mask = (bytes[1] & 0b10000000) != 0; // must be true, "All messages from the client to the server have this bit set"
                int opcode = bytes[0] & 0b00001111; // expecting 1 - text message
                ulong offset = 2,
                      msgLen = bytes[1] & (ulong)0b01111111;

                if (msgLen == 126) {
                    // bytes are reversed because websocket will print them in Big-Endian, whereas
                    // BitConverter will want them arranged in little-endian on windows
                    msgLen = BitConverter.ToUInt16(new byte[] { bytes[3], bytes[2] }, 0);
                    offset = 4;
                } else if (msgLen == 127) {
                    // To test the below code, we need to manually buffer larger messages — since the NIC's autobuffering
                    // may be too latency-friendly for this code to run (that is, we may have only some of the bytes in this
                    // websocket frame available through client.Available).
                    msgLen = BitConverter.ToUInt64(new byte[] { bytes[9], bytes[8], bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2] },0);
                    offset = 10;
                }

                if (msgLen == 0) {
                    Console.WriteLine("msgLen == 0");
                } else if (mask) {
                    byte[] decoded = new byte[msgLen];
                    byte[] masks = new byte[4] { bytes[offset], bytes[offset + 1], bytes[offset + 2], bytes[offset + 3] };
                    offset += 4;

                    for (ulong i = 0; i < msgLen; ++i)
                        decoded[i] = (byte)(bytes[offset + i] ^ masks[i % 4]);

                    string text = Encoding.UTF8.GetString(decoded);
                    Console.WriteLine("{0}", text);
                } else
                    Console.WriteLine("mask bit not set");

                Console.WriteLine();
            }
        }
    }
}

client.html

html
<!doctype html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="styles.css" />
    <script src="client.js" defer></script>
  </head>
  <body>
    <h2>WebSocket Test</h2>
    <textarea cols="60" rows="6"></textarea>
    <button>send</button>
    <div id="output"></div>
  </body>
</html>

styles.css

css
textarea {
  vertical-align: bottom;
}
#output {
  overflow: auto;
}
#output > p {
  overflow-wrap: break-word;
}
#output span {
  color: blue;
}
#output span.error {
  color: red;
}

client.js

js
// http://www.websocket.org/echo.html
const button = document.querySelector("button");
const output = document.querySelector("#output");
const textarea = document.querySelector("textarea");
const wsUri = "ws://127.0.0.1/";
const websocket = new WebSocket(wsUri);

button.addEventListener("click", onClickButton);

websocket.onopen = (e) => {
  writeToScreen("CONNECTED");
  doSend("WebSocket rocks");
};

websocket.onclose = (e) => {
  writeToScreen("DISCONNECTED");
};

websocket.onmessage = (e) => {
  writeToScreen(`<span>RESPONSE: ${e.data}</span>`);
};

websocket.onerror = (e) => {
  writeToScreen(`<span class="error">ERROR:</span> ${e.data}`);
};

function doSend(message) {
  writeToScreen(`SENT: ${message}`);
  websocket.send(message);
}

function writeToScreen(message) {
  output.insertAdjacentHTML("afterbegin", `<p>${message}</p>`);
}

function onClickButton() {
  const text = textarea.value;

  text && doSend(text);
  textarea.value = "";
  textarea.focus();
}