Working with WebSocket in Asp.Net Core
Hello, in today's article, we will talk about WebSocket. But first, we need to look at communication in server-client architecture. Web applications (clients) communicate with the server via HTTP protocol with request/response logic. When the client requests something from the server, it opens a connection and sends a request. The server receives the request and takes the necessary actions. The server returns the answer to the client. This traffic continues in this way all the time. The client has to open a connection for each request. And if the server has new information to give to the client, it cannot send it directly to the client, instead, it must wait for a request from the client.
This redundancy can fill an entire bandwidth. Worse still, this can almost prevent the client from being flooded with secondary information. This can cause great losses in online financial transactions that need instant speed.
Web developers have been developing systems for many years to overcome this limitation of HTTP and to provide more data transfer during any connection. But what they really wanted to do was to allow both sides to exchange data to each other without breaking the connection between the server and the client.
The solution standards that will seem like a cure for this need came from HTML5, which is almost set. This new protocol that came with HTML5 was WebSocket. WebSocket is a system that enables the Internet to perform complex communication operations in real-time. WebSocket allows the client to establish a connection and maintain this connection as long as it wants, and both receive and send data both ways.
WebSocket Example in Asp.Net Core
A simple script knowledge and an intermediate knowledge of .Net Core are sufficient to use Web Socket. The logic is simple.
First of all, we open a core web application project named WebsocketUsimi. Then we add the AspNetCore.WebSockets package to the project via Nuget.
Install-Package Microsoft.AspNetCore.WebSockets -Version 2.2.1
First, we will make the necessary settings in the Configure method in the Startup.cs class of our application. We declare that we will use WebSocket at runtime. We will open the communication between the client and the server and check the messages of the incoming requests. Our goal is to capture the incoming message and say something to it.
In the codes, we first look at the Configure method. We inform the environment that we will use Web Socket with the UseWebSockets() function. When the application is up, we start to listen to incoming requests. If this incoming request is a Web Socket request (which must be a request that starts with the ws:// protocol), we capture a socket object and call the function named Talk (again asynchronously, of course). process is continuing.
Within the talk operation, there are codes required for printing the message from the client to the terminal and sending back a randomly generated number value. A method named ReceiveAsync is used to capture the message sent by the client. The first parameter of the method takes the incoming content into a Byte array. If the communication is open (this is controlled by CloseStatus.HasValue), it is sufficient to convert this byte content to string. The SendAsync method is used to send a message to the connected client over the socket. Traffic works with byte arrays, so UTF8-based Encoding mechanisms are used for string operations.
We set the port we will use in Program.cs. We will broadcast from http://localhost:5556 and our clients will be able to establish websocket based communication using ws://localhost:5556/nokya.
In the example project, we will communicate with socket using javascript on the index.html page as a client.
It works when the btnConnect button is pressed in our codes. As it should be noted, a WebSocket object is being generated. The address ws://localhost:5556/nokya is used as the address. Note that the address starts with ws. Some events are defined for the created WebSocket object. When the connection is opened, closed, a message is received from the server, or an error occurs. The send function of the WebSocket object is used to send a message to the server.
I hope it was useful.