示例#1
0
        /// <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);
        }
示例#2
0
        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);
                }
            }
        }
示例#3
0
        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);
                }
            }
        }
示例#4
0
 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);
             }
         }
     }
 }
示例#5
0
 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);
             }
         }
     }
 }
示例#6
0
 public async Task SendMessageAsync(string socketId, Message message)
 {
     await SendMessageAsync(WebSocketConnectionManager.GetSocketById(socketId), message).ConfigureAwait(false);
 }
示例#7
0
 /// <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);
 }
示例#8
0
 /// <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;
 }