QuickStart
1. Create a new Window Forms Application
2. Drop a TsgcWebSocketServer onto a Form.
3. On Events Tab, Double click OnMessage Event, and type following code:
4. Drop a Button onto the Form, Double Click and type this code:
SSL
Server can be configured to use SSL Certificates, in order to get a Production Server with a server certificate, you must purchase a Certificate from a well known provider: Namecheap, godaddy, Thawte... For testing purposes you can use a self-signed certificate (check out in Demos/Chat which uses a self-signed certificate).
Certificate must be in PEM format, PEM (from Privacy Enhanced Mail) is defined in RFCs 1421 through 1424, this is a container format that may include just the public certificate (such as with Apache installs, and CA certificate files /etc/ssl/certs), or may include an entire certificate chain including public key, private key, and root certificates. To create a single pem certificate, just open your private key file, copy the contents and paste on certificate file.
Simple SSL Configuration
Example: configure SSL in IP 127.0.0.1 and Port 443
WatchDog
Once server is started and OnShutdown event is fired, sometimes server can stopped for any reason. If you want to restart server after an unexpected close, you can use WatchDog property.
If WatchDog is enabled, when server detects a Shutdown, WatchDog try to restart again every X seconds until server is active again.
Example: restart every 10 seconds after an unexpected stop with unlimited attempts.
HeartBeat
Once your client has connected to server, sometimes connection can be closed due to poor signal, connection errors... use Heartbeat to keep connection alive.
HeartBeat property allows to send a Ping every X seconds to maintain connection alive. Some servers, close TCP connections if there is no data exchanged between peers. HeartBeat solves this problem, sending a ping every a specific interval. Usually this is enough to maintain a connection active, but you can set a TimeOut interval if you want to close connection if a response from client is not received after X seconds.
Example: send a ping to all connected clients every 30 seconds
Authentication
TsgcWebSocket server supports 3 types of Authentications:
- Basic: read an HTTP Header during WebSocket HandShake with User and Password encoded as Basic Authorization.
- Session: first client request an HTTP session to server and if server returns a session this is passed in GET HTTP Header of WebSocket HandShake. (* own authorization method for sgcWebSockets library).
- URL: read request authorization using GET HTTP Header of WebSocket HandShake. (* own authorization method for sgcWebSockets library).
Send Messages
Once client has connected to server, server can send text messages. To send a Text Message, just call WriteData() method to send a message to a single client or use Broadcast to send a message to all clients.
Call To WriteData() method and send a Text message.
Receive Messages
When server receives a Text Message, OnMessage event is fired, just read Text parameter to know the string of message received.
By default, server uses neAsynchronous method to dispatch OnMessage event, this means that this event is executed on the context of Main Thread, so it's thread-safe to update any control of a form for example.
If your server receives lots of messages or you need to control the synchronization with other threads, set NotifyEvents property to neNoSync, this means that OnMessage event will be executed on the context of connection thread, so if you require to update any control of a form or access shared objects, you must implement your own synchronization methods.
sgcWebSockets .NET