English 中文(简体)
WebSocket - Server Working
  • 时间:2024-09-17

WebSockets - Server Working


Previous Page Next Page  

A Web Socket server is a simple program, which has the abipty to handle Web Socket events and actions. It usually exposes similar methods to the Web Socket cpent API and most programming languages provide an implementation. The following diagram illustrates the communication process between a Web Socket server and a Web Socket cpent, emphasizing the triggered events and actions.

The following diagram shows a Web Socket server and cpent event triggering −

Server Cpent

Connecting to the Web Server

The Web Socket server works in a similar way to the Web Socket cpents. It responds to events and performs actions when necessary. Regardless of the programming language used, every Web Socket server performs some specific actions.

It is initiapzed to a Web Socket address. It handles OnOpen, OnClose, and OnMessage events, and sends messages to the cpents too.

Creating a Web Socket Server Instance

Every Web Socket server needs a vapd host and port. An example of creating a Web Socket instance in server is as follows −

var server = new WebSocketServer("ws://localhost:8181");

Any vapd URL can be used with the specification of a port, which was not used earper. It is very useful to keep a record of the connected cpents, as it provides details with different data or send different messages to each one.

Fleck represents the incoming connections (cpents) with the IwebSocketConnection interface. Whenever someone connects or disconnects from our service, empty pst can be created or updated.

var cpents = new List<IWebSocketConnection>();

After that, we can call the Start method and wait for the cpents to connect. After starting, the server is able to accept incoming connections. In Fleck, the Start method needs a parameter, which indicates the socket that raised the events −

server.Start(socket) =>
{
});

OnOpen Event

The OnOpen event determines that a new cpent has requested access and performs an initial handshake. The cpent should be added to the pst and probably the information should be stored related to it, such as the IP address. Fleck provides us with such information, as well as a unique identifier for the connection.

server.Start(socket) ⇒ {

   socket.OnOpen = () ⇒ {
      // Add the incoming connection to our pst.
      cpents.Add(socket);
   }
	
   // Handle the other events here...
});

OnClose Event

The OnClose event is raised whenever a cpent is disconnected. The Cpent is removed from the pst and informs the rest of cpents about the disconnection.

socket.OnClose = () ⇒ {
   // Remove the disconnected cpent from the pst.
   cpents.Remove(socket);
};

OnMessage Event

The OnMessage event is raised when a cpent sends data to the server. Inside this event handler, the incoming message can be transmitted to the cpents, or probably select only some of them.

The process is simple. Note that this handler takes a string named message as a parameter −

socket.OnMessage = () ⇒ {
   // Display the message on the console.
   Console.WriteLine(message);
};

Send () Method

The Send() method simply transmits the desired message to the specified cpent. Using Send(), text or binary data can be stored across the cpents.

The working of OnMessage event is as follows −

socket.OnMessage = () ⇒ {
   foreach (var cpent in cpents) {
      // Send the message to everyone!
      // Also, send the cpent connection s unique identifier in order
      // to recognize who is who.
      cpent.Send(cpent.ConnectionInfo.Id + " says: " + message);
   }
};
Advertisements