Class WolfClient
A default base wolf client implementation.
Inheritance
Namespace: TehGM.Wolfringo
Assembly: Wolfringo.Core.dll
Syntax
public class WolfClient : IWolfClient, IWolfClientCacheAccessor, IDisposable
Remarks
This implementation, together with IHostedWolfClient from Wolfringo.Hosting package, is the default intended way to use Wolfringo library. It's based on message and response serializers, which combined with constructor Dependency Injection provide high degree of customizability.
Custom SocketIO client SocketClient is used by this library. This client only provides functionality required for Wolf, and may not be suitable for other scenarios.
This implementation automatically handles cache updates whenever a correct type of message or response is sent or received. For this reason, it's important to be careful when overriding OnMessageSentAsync(IWolfMessage, IWolfResponse, SerializedMessageData, CancellationToken) and OnMessageReceivedAsync(IWolfMessage, SerializedMessageData, CancellationToken), as not calling base implementation (or not implementing replacement behaviour) might cause functionality loss.
Constructors
| Improve this Doc View SourceWolfClient()
Creates a new wolf client instance.
Declaration
[Obsolete("Use WolfClientBuilder instead")]
public WolfClient()
WolfClient(ILogger)
Creates a new wolf client instance.
Declaration
[Obsolete("Use WolfClientBuilder instead")]
public WolfClient(ILogger log)
Parameters
| Type | Name | Description |
|---|---|---|
| Microsoft.Extensions.Logging.ILogger | log | Logger to use with this client. If null, no messages will be logged. |
WolfClient(ILoggerFactory)
Creates a new wolf client instance.
Declaration
[Obsolete("Use WolfClientBuilder instead")]
public WolfClient(ILoggerFactory logFactory)
Parameters
| Type | Name | Description |
|---|---|---|
| Microsoft.Extensions.Logging.ILoggerFactory | logFactory | Logger factory to use with this client. If null, no messages will be logged. |
WolfClient(IServiceProvider, WolfClientOptions)
Creates a new wolf client instance.
Declaration
public WolfClient(IServiceProvider services, WolfClientOptions options)
Parameters
| Type | Name | Description |
|---|---|---|
| System.IServiceProvider | services | Services provider to resolve dependencies from. |
| WolfClientOptions | options | Options for this client. |
Properties
| Improve this Doc View SourceCache
Caches container.
Declaration
protected IWolfClientCache Cache { get; set; }
Property Value
| Type | Description |
|---|---|
| IWolfClientCache |
CallbackDispatcher
Callbacks dispatcher used by this WOLF client.
Declaration
protected MessageCallbackDispatcher CallbackDispatcher { get; }
Property Value
| Type | Description |
|---|---|
| MessageCallbackDispatcher |
CurrentUserID
Currently logged in user. Null if not connected.
Declaration
public uint? CurrentUserID { get; protected set; }
Property Value
| Type | Description |
|---|---|
| System.Nullable<System.UInt32> |
Device
Device to pass to the server when connecting.
Declaration
public WolfDevice Device { get; }
Property Value
| Type | Description |
|---|---|
| WolfDevice |
DisposablesHandler
Handler for tracking services that should be disposed when this WolfClient is being disposed.
Declaration
protected DisposableServicesHandler DisposablesHandler { get; }
Property Value
| Type | Description |
|---|---|
| DisposableServicesHandler |
IgnoreOwnChatMessages
Whether the client should skip raising events for messages it sent.
Declaration
protected bool IgnoreOwnChatMessages { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
IsConnected
Is this client currently connected?
Declaration
public bool IsConnected { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
Log
Logger for all log messages.
Declaration
protected ILogger Log { get; }
Property Value
| Type | Description |
|---|---|
| Microsoft.Extensions.Logging.ILogger |
MessageSerializers
Message serializers mapping used when serializing and deserializing messages.
Declaration
protected ISerializerProvider<string, IMessageSerializer> MessageSerializers { get; }
Property Value
| Type | Description |
|---|---|
| ISerializerProvider<System.String, IMessageSerializer> |
ResponseSerializers
Response serializers mapping used when deserializing responses.
Declaration
protected ISerializerProvider<Type, IResponseSerializer> ResponseSerializers { get; }
Property Value
| Type | Description |
|---|---|
| ISerializerProvider<System.Type, IResponseSerializer> |
ResponseTypeResolver
Response type resolver used when deserializing responses.
Declaration
protected IResponseTypeResolver ResponseTypeResolver { get; }
Property Value
| Type | Description |
|---|---|
| IResponseTypeResolver |
SocketClient
Socket client used by this WOLF client.
Declaration
protected ISocketClient SocketClient { get; }
Property Value
| Type | Description |
|---|---|
| ISocketClient |
Token
Token used with the connection.
Declaration
protected string Token { get; }
Property Value
| Type | Description |
|---|---|
| System.String |
Url
URL of the server.
Declaration
public string Url { get; }
Property Value
| Type | Description |
|---|---|
| System.String |
Methods
| Improve this Doc View SourceAddMessageListener(IMessageCallback)
Adds event listener.
Declaration
public void AddMessageListener(IMessageCallback listener)
Parameters
| Type | Name | Description |
|---|---|---|
| IMessageCallback | listener | Callback to invoke on event. |
BuildDefaultServiceProvider(ILogger)
Builds default service provider. Used to temporarily support obsolete non-builder constructors.
Declaration
[Obsolete]
protected static IServiceProvider BuildDefaultServiceProvider(ILogger log = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Microsoft.Extensions.Logging.ILogger | log | A logger to add to the services. If null, logging will be disabled. |
Returns
| Type | Description |
|---|---|
| System.IServiceProvider | A System.IServiceProvider with default services added. |
Clear()
Clears all connection-bound variables.
Declaration
protected virtual void Clear()
ConnectAsync(CancellationToken)
Connects to the server.
Declaration
public Task ConnectAsync(CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| System.Threading.CancellationToken | cancellationToken | Token to abort entire connection. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task |
ConnectAsync(WolfDevice, CancellationToken)
Declaration
public async Task ConnectAsync(WolfDevice device, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| WolfDevice | device | Device to connect as. |
| System.Threading.CancellationToken | cancellationToken | Cancellation token that can be used for Task cancellation. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task |
DisconnectAsync(CancellationToken)
Disconnects from the server.
Declaration
public Task DisconnectAsync(CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| System.Threading.CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task |
Dispose()
Declaration
public virtual void Dispose()
OnMessageReceivedAsync(IWolfMessage, SerializedMessageData, CancellationToken)
Internal method for handling additional actions on received message.
Declaration
protected virtual Task OnMessageReceivedAsync(IWolfMessage message, SerializedMessageData rawMessage, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| IWolfMessage | message | Received message. |
| SerializedMessageData | rawMessage | Raw received message. |
| System.Threading.CancellationToken | cancellationToken | Cancellation token that can be used for Task cancellation. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task |
Remarks
This method is invoked after client parses current user (for welcome) and caches the entities.
OnMessageSentAsync(IWolfMessage, IWolfResponse, SerializedMessageData, CancellationToken)
Internal method for handling additional actions on sent message.
Declaration
protected virtual Task OnMessageSentAsync(IWolfMessage message, IWolfResponse response, SerializedMessageData rawResponse, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| IWolfMessage | message | Sent message. |
| IWolfResponse | response | Response received. |
| SerializedMessageData | rawResponse | Raw response data. |
| System.Threading.CancellationToken | cancellationToken | Cancellation token that can be used for Task cancellation. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task |
Remarks
This method is invoked after client parses current user (for login/logout), populates chat message body data, and caches the entities.
RemoveMessageListener(IMessageCallback)
Removes event listener.
Declaration
public void RemoveMessageListener(IMessageCallback listener)
Parameters
| Type | Name | Description |
|---|---|---|
| IMessageCallback | listener | Callback to remove. |
SendAsync<TResponse>(IWolfMessage, CancellationToken)
Sends message, and waits for response from the server.
Declaration
public async Task<TResponse> SendAsync<TResponse>(IWolfMessage message, CancellationToken cancellationToken = default(CancellationToken))
where TResponse : IWolfResponse
Parameters
| Type | Name | Description |
|---|---|---|
| IWolfMessage | message | Message to send. |
| System.Threading.CancellationToken | cancellationToken | Cancellation token that can be used for Task cancellation. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task<TResponse> | Sending response. |
Type Parameters
| Name | Description |
|---|---|
| TResponse | Response type to use for casting of response. |
Remarks
If client uses ResponseTypeResolver, the type of response provided with
ResponseTypeAttribute on message will be used for deserialization,
and TResponse will be used only for casting. If ResponseTypeAttribute is not set on
message, TResponse will be used for deserialization as normal.
Exceptions
| Type | Condition |
|---|---|
| MessageSendingException | Server responded with error. |
TryLogMessageTrace(SocketMessageEventArgs, String)
Logs socket message event with trace log level.
Declaration
protected void TryLogMessageTrace(SocketMessageEventArgs e, string keyword)
Parameters
| Type | Name | Description |
|---|---|---|
| SocketMessageEventArgs | e | Received socket event. |
| System.String | keyword | Keyword to prepend in log message, for example "Sent" or "Received". |
TryParseCommandEvent(SocketMessage, out String, out JToken)
Tries to parse command and payload object.
Declaration
protected static bool TryParseCommandEvent(SocketMessage message, out string command, out JToken payload)
Parameters
| Type | Name | Description |
|---|---|---|
| SocketMessage | message | Received socket message. |
| System.String | command | Parsed message command; null if not a command message. |
| Newtonsoft.Json.Linq.JToken | payload | Extracted payload. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if command parsed and is not null; otherwise false. |
Events
| Improve this Doc View SourceConnected
Rasied when the client connects to the server.
Declaration
public event EventHandler Connected
Event Type
| Type | Description |
|---|---|
| System.EventHandler |
Disconnected
Raised when the client disconnects from the server.
Declaration
public event EventHandler Disconnected
Event Type
| Type | Description |
|---|---|
| System.EventHandler |
ErrorRaised
Raised when an error has occured.
Declaration
public event EventHandler<UnhandledExceptionEventArgs> ErrorRaised
Event Type
| Type | Description |
|---|---|
| System.EventHandler<System.UnhandledExceptionEventArgs> |
MessageReceived
Raised when the client receives an event from the server.
Declaration
public event EventHandler<WolfMessageEventArgs> MessageReceived
Event Type
| Type | Description |
|---|---|
| System.EventHandler<WolfMessageEventArgs> |
MessageSent
Raised when the client sends a message to the server.
Declaration
public event EventHandler<WolfMessageSentEventArgs> MessageSent
Event Type
| Type | Description |
|---|---|
| System.EventHandler<WolfMessageSentEventArgs> |
Explicit Interface Implementations
| Improve this Doc View SourceIWolfClientCacheAccessor.GetCachedAchievement(WolfLanguage, UInt32)
Get achievement from cache.
Declaration
WolfAchievement IWolfClientCacheAccessor.GetCachedAchievement(WolfLanguage language, uint id)
Parameters
| Type | Name | Description |
|---|---|---|
| WolfLanguage | language | Language of achievement's translations. |
| System.UInt32 | id | ID of the achievement. |
Returns
| Type | Description |
|---|---|
| WolfAchievement | Cached achievement if found in requested language; otherwise null. |
IWolfClientCacheAccessor.GetCachedCharm(UInt32)
Get charm from cache.
Declaration
WolfCharm IWolfClientCacheAccessor.GetCachedCharm(uint id)
Parameters
| Type | Name | Description |
|---|---|---|
| System.UInt32 | id | ID of the charm. |
Returns
| Type | Description |
|---|---|
| WolfCharm | Cached charm if found; otherwise null. |
IWolfClientCacheAccessor.GetCachedGroup(String)
Get group from cache.
Declaration
WolfGroup IWolfClientCacheAccessor.GetCachedGroup(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | name | Name of the group. |
Returns
| Type | Description |
|---|---|
| WolfGroup | Cached group if found; otherwise null. |
IWolfClientCacheAccessor.GetCachedGroup(UInt32)
Get group from cache.
Declaration
WolfGroup IWolfClientCacheAccessor.GetCachedGroup(uint id)
Parameters
| Type | Name | Description |
|---|---|---|
| System.UInt32 | id | ID of the group. |
Returns
| Type | Description |
|---|---|
| WolfGroup | Cached group if found; otherwise null. |
IWolfClientCacheAccessor.GetCachedUser(UInt32)
Get user from cache.
Declaration
WolfUser IWolfClientCacheAccessor.GetCachedUser(uint id)
Parameters
| Type | Name | Description |
|---|---|---|
| System.UInt32 | id | ID of the user. |
Returns
| Type | Description |
|---|---|
| WolfUser | Cached user if found; otherwise null. |