-
Notifications
You must be signed in to change notification settings - Fork 5
FAQ
"Access to the path is denied" error
Detecting Connected and Disconnected events
ConnectToServer vs TryConnectToServer and SubscribeOnServer vs TrySubscribeOnServer
Meta.Lib
uses ILogger
interface from Microsoft.Extensions.Logging
namespace to log its internal events and errors. The TestServer
demo application contains an example of how to create an ILogger
implementation for the console application.
You may receive this message if the server-side MetaPubSub
runs in a Windows process with elevated permissions but the client process is not. In this case, you need to define your own delegate method to create NamedPipeServerStream
.
Note: your server project must be created for .Net Framework, not .Net Core. See TestServer
example application for complete source codes.
hub.StartServer("Meta", () =>
{
var pipeSecurity = new PipeSecurity();
pipeSecurity.AddAccessRule(new PipeAccessRule(
new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null),
PipeAccessRights.FullControl,
AccessControlType.Allow));
var pipe = new NamedPipeServerStream("Meta", PipeDirection.InOut, 32,
PipeTransmissionMode.Message, PipeOptions.Asynchronous, 4096, 4096, pipeSecurity);
return pipe;
});
There are four built-in message you can subscribe for:
-
RemoteClientConnectedEvent
- publishes on the server hub when a client connects -
RemoteClientDisconnectedEvent
- publishes on the server hub when a client disconnects -
ConnectedToServerEvent
- publishes on the client hub when it connects to the server -
DisconnectedFromServerEvent
- publishes on the client hub when it disconnects from the server
You can subscribe RemoteClientConnectedEvent
and RemoteClientDisconnectedEvent
on the server hub. These events have following properties:
-
DateTime Timestamp
- date and time when connection/disconnection event has happened (local server time) -
int TotalClientsCount
- total count of the clients after the event
You can subscribe ConnectedToServerEvent
on the client hub. It has the following properties:
-
DateTime Timestamp
- date and time when connection/disconnection event has happened (local client time)
You can subscribe DisconnectedFromServerEvent
on the client hub. It has the following properties:
-
DateTime Timestamp
- date and time when connection/disconnection event has happened (local client time) -
bool LostConnection
- if true than connection was lost for some reason, otherwise it has been closed usingDisconnectFromServer
method
Example:
// server hub
var serverHub = new MetaPubSub();
serverHub.StartServer("Meta");
serverHub.Subscribe<RemoteClientConnectedEvent>(ConnectedHandler);
serverHub.Subscribe<RemoteClientDisconnectedEvent>(DisconnectedHandler);
// client hub
var hub = new MetaPubSub();
hub.Subscribe<ConnectedToServerEvent>(ConnectedToServerHandler);
hub.Subscribe<DisconnectedFromServerEvent>(DisconnectedFromServerHandler);
await hub.ConnectToServer("Meta");
await hub.DisconnectFromServer();
When a client loses its connection to the server, it can automatically re-establish the connection. So you don't need the logic for listening for Disconnected/Connected events and resubscribing all required messages. However, if the Server is not yet started calling the SubscribeOnServer
method will result in an exception. To address this, we could implement logic to wait for a connection and subscribe to the necessary events. However, this would require duplicating the code for each class that subscribes to server events, leading to code bloat.
To simplify this process, we can use the TryConnect
and TrySubscribeOnServer
methods.
-
TryConnect
establishes a connection to the server as soon as it's available, returning true if the connection is established during the call, and false otherwise. -
TrySubscribeOnServer
returns false if the server isn't connected, and makes the subscription automatically once the server connection is established.
With these methods, you don't need to worry about whether there's a connection to the server at the time of the method call, making it easier to handle connection issues in your code.