Connecting to the web server
The WebSocket server works in a similar way to the WebSocket clients. It responds to events and performs actions when necessary.Regardless of the programming language you use, every WebSocket server performs some specific actions. It is initialized to a WebSocket address, it handles OnOpen
, OnClose
and OnMessage
events, and sends messages to the clients, too.
Creating the WebSocket server instance
Every WebSocket server needs a valid host and port. Here is how we create a WebSocketServer instance in Fleck:
var server = new WebSocketServer("ws://localhost:8181");
You can type any valid URL you'd like and specify a port that is not in use.
It is very useful to keep a record of the connected clients, as you may need to provide them with different data or send different messages to each one.
Fleck represents the incoming connections (clients) with the IWebSocketConnection
interface. We can create an empty list and update it whenever someone connects or disconnects from...