/// <summary> /// Called when a client has connected to the server. /// </summary> /// <param name="socket">The web-socket of the client.</param> /// <returns>Awaitable Task.</returns> public virtual async Task OnConnected(WebSocket socket) { WebSocketConnectionManager.AddSocket(socket); await SendMessageAsync(socket, new Message() { MessageType = MessageType.ConnectionEvent, Data = WebSocketConnectionManager.GetId(socket) }).ConfigureAwait(false); }
public async Task InvokeClientMethodToGroupAsync(string groupID, string methodName, params object[] arguments) { var sockets = WebSocketConnectionManager.GetAllFromGroup(groupID); if (sockets != null) { foreach (var id in sockets) { await InvokeClientMethodAsync(id, methodName, arguments); } } }
public async Task SendMessageToGroupAsync(string groupID, Message message) { var sockets = WebSocketConnectionManager.GetAllFromGroup(groupID); if (sockets != null) { foreach (var socket in sockets) { await SendMessageAsync(socket, message); } } }
public async Task InvokeClientMethodToAllAsync(string methodName, params object[] arguments) { foreach (var pair in WebSocketConnectionManager.GetAll()) { try { if (pair.Value.State == WebSocketState.Open) { await InvokeClientMethodAsync(pair.Key, methodName, arguments).ConfigureAwait(false); } } catch (WebSocketException e) { if (e.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely) { await OnDisconnected(pair.Value); } } } }
public async Task SendMessageToAllAsync(Message message) { foreach (var pair in WebSocketConnectionManager.GetAll()) { try { if (pair.Value.State == WebSocketState.Open) { await SendMessageAsync(pair.Value, message).ConfigureAwait(false); } } catch (WebSocketException e) { if (e.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely) { await OnDisconnected(pair.Value); } } } }
public async Task SendMessageAsync(string socketId, Message message) { await SendMessageAsync(WebSocketConnectionManager.GetSocketById(socketId), message).ConfigureAwait(false); }
/// <summary> /// Called when a client has disconnected from the server. /// </summary> /// <param name="socket">The web-socket of the client.</param> /// <returns>Awaitable Task.</returns> public virtual async Task OnDisconnected(WebSocket socket) { await WebSocketConnectionManager.RemoveSocket(WebSocketConnectionManager.GetId(socket)).ConfigureAwait(false); }
/// <summary> /// Initializes a new instance of the <see cref="WebSocketHandler"/> class. /// </summary> /// <param name="webSocketConnectionManager">The web socket connection manager.</param> /// <param name="methodInvocationStrategy">The method invocation strategy used for incoming requests.</param> public WebSocketHandler(WebSocketConnectionManager webSocketConnectionManager, MethodInvocationStrategy methodInvocationStrategy) { _jsonSerializerSettings.Converters.Insert(0, new PrimitiveJsonConverter()); WebSocketConnectionManager = webSocketConnectionManager; MethodInvocationStrategy = methodInvocationStrategy; }