Get to know MDN better
If you would like to use the WebSocket API, it is useful if you have a server. In this article I will show you how to write one in C#. You can do it in any server-side language, but to keep things simple and more understandable, I chose Microsoft's language.
This server conforms to RFC 6455, so it will only handle connections from Chrome version 16, Firefox 11, IE 10 and over.
WebSockets communicate over a TCP (Transmission Control Protocol) connection. Luckily, C# has a TcpListener class which does as the name suggests. It is in the System.Net.Sockets namespace.
Note: It is a good idea to include the namespace with the using keyword in order to write less. It allows usage of a namespace's classes without typing the full namespace every time.
Constructor:
localAddr specifies the IP of the listener, and port specifies the port.
Note: To create an IPAddress object from a string, use the Parse static method of IPAddress.
Methods:
Here's a barebones server implementation:
Methods:
Properties:
Methods:
Writes bytes from buffer, offset and size determine length of message.
Reads bytes to buffer. offset and size determine the length of the message.
Let us extend our example.
When a client connects to a server, it sends a GET request to upgrade the connection to a WebSocket from a simple HTTP request. This is known as handshaking.
This sample code can detect a GET from the client. Note that this will block until the first 3 bytes of a message are available. Alternative solutions should be investigated for production environments.
The response is easy to build, but might be a little difficult to understand. The full explanation of the Server handshake can be found in RFC 6455, section 4.2.2. For our purposes, we'll just build a simple response.
You must:
After a successful handshake, the client will send encoded messages to the server.
If we send "MDN", we get these bytes:
129 131 61 84 35 6 112 16 109Let's take a look at what these bytes mean.
The first byte, which currently has a value of 129, is a bitfield that breaks down as such:
| 1 | 0 | 0 | 0 | 0x1=0001 |
The second byte, which currently has a value of 131, is another bitfield that breaks down as such:
| 1 | 0x83=0000011 |
Note: Because the first bit is always 1 for client-to-server messages, you can subtract 128 from this byte to get rid of the MASK bit.
Note that the MASK bit is set in our message. This means that the next four bytes (61, 84, 35, and 6) are the mask bytes used to decode the message. These bytes change with every message.
The remaining bytes are the encoded message payload.
D_i = E_i XOR M_(i mod 4)
where D is the decoded message array, E is the encoded message array, M is the mask byte array, and i is the index of the message byte to decode.
Example in C#:
This page was last modified on Jun 19, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.