public void AddSocket(WebSocketItem socket)
        {
            string sId = CreateConnectionId();

            while (!_sockets.TryAdd(sId, socket))
            {
                sId = CreateConnectionId();
            }
        }
Ejemplo n.º 2
0
        public async Task SendMessageAsync(WebSocketItem socket, string message)
        {
            if (socket.Socket.State != WebSocketState.Open)
            {
                return;
            }

            await socket.Socket.SendAsync(buffer : new ArraySegment <byte>(array: Encoding.UTF8.GetBytes(message),
                                                                           offset: 0,
                                                                           count: message.Length),
                                          messageType : WebSocketMessageType.Text,
                                          endOfMessage : true,
                                          cancellationToken : CancellationToken.None);
        }
        public async Task Invoke(HttpContext context)
        {
            if (!context.WebSockets.IsWebSocketRequest)
            {
                return;
            }

            var socket = await context.WebSockets.AcceptWebSocketAsync();

            var auth = await context.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);

            var userId = string.Empty;

            if (auth.Succeeded)
            {
                userId = auth.Principal.Identity.Name;
            }
            if (string.IsNullOrEmpty(userId))
            {
                await socket.CloseOutputAsync(WebSocketCloseStatus.ProtocolError, "auth error", CancellationToken.None);

                return;
            }
            var socketItem = new WebSocketItem(socket);

            socketItem.Items.Add("userId", userId);
            await _webSocketHandler.OnConnected(socketItem);

            await Receive(socket, async (result, buffer) =>
            {
                if (result.MessageType == WebSocketMessageType.Text)
                {
                    await _webSocketHandler.ReceiveAsync(socketItem, result, buffer);
                    return;
                }

                else if (result.MessageType == WebSocketMessageType.Close)
                {
                    await _webSocketHandler.OnDisconnected(socketItem);
                    return;
                }
            });

            //TODO - investigate the Kestrel exception thrown when this is the last middleware
            //await _next.Invoke(context);
        }
 public string GetId(WebSocketItem socket)
 {
     return(_sockets.FirstOrDefault(p => p.Value == socket).Key);
 }
Ejemplo n.º 5
0
        public override async Task ReceiveAsync(WebSocketItem socket, WebSocketReceiveResult result, byte[] buffer)
        {
            string message = Encoding.UTF8.GetString(buffer, 0, result.Count);

            await SendMessageToAllAsync(message);
        }
Ejemplo n.º 6
0
 public abstract Task ReceiveAsync(WebSocketItem socket, WebSocketReceiveResult result, byte[] buffer);
Ejemplo n.º 7
0
 public virtual async Task OnDisconnected(WebSocketItem socket)
 {
     await WebSocketConnectionManager.RemoveSocket(WebSocketConnectionManager.GetId(socket));
 }
Ejemplo n.º 8
0
 public virtual async Task OnConnected(WebSocketItem socket)
 {
     WebSocketConnectionManager.AddSocket(socket);
 }