Beispiel #1
0
        public async Task BroadcastAllAsync(byte[] buffer, CustomWebSocket userWebSocket, ICustomWebSocketFactory wsFactory)
        {
            var all = wsFactory.All();

            foreach (var uws in all)
            {
                await uws.WebSocket.SendAsync(new ArraySegment <byte>(buffer, 0, buffer.Length), WebSocketMessageType.Text, true, CancellationToken.None);
            }
        }
Beispiel #2
0
        public async Task SendDisconnectMessageAsync(CustomWebSocket userWebSocket, ICustomWebSocketFactory wsFactory)
        {
            var webSocket = userWebSocket.WebSocket;
            var msg       = new CustomWebSocketMessage
            {
                MessageDateTime      = DateTime.Now,
                Type                 = WebSocketMessageType.Text,
                Username             = userWebSocket.UserId,
                WebSocketMessageType = "onUserLeave"
            };

            string serialisedMessage = JsonConvert.SerializeObject(msg);

            byte[] bytes = Encoding.ASCII.GetBytes(serialisedMessage);
            await BroadcastInGroupAsync(bytes, userWebSocket, wsFactory);
        }
 public void Add(CustomWebSocket webSocket)
 {
     if (WebSockets.TryGetValue(webSocket.GroupId, out var value))
     {
         value.WebSockets.Add(webSocket);
     }
     else
     {
         var newWebSocketGroup = new CustomWebSocketGroup()
         {
             GroupId = webSocket.GroupId, FriendlyName = webSocket.FriendlyName
         };
         newWebSocketGroup.WebSockets = new List <CustomWebSocket> {
             webSocket
         };
         this.WebSockets.Add(webSocket.GroupId, newWebSocketGroup);
     }
 }
 public List <CustomWebSocket> AllInGroup(CustomWebSocket client)
 {
     return(WebSockets[client.GroupId].WebSockets);
 }
Beispiel #5
0
        public async Task HandleMessageAsync(WebSocketReceiveResult result, IEnumerable <byte> buffer, CustomWebSocket userWebSocket, ICustomWebSocketFactory wsFactory)
        {
            var msg = Encoding.ASCII.GetString(buffer.ToArray()).TrimEnd('\0');

            try
            {
                var incomingMessage = JsonConvert.DeserializeObject <IncomingWebSocketMessageWrapper>(msg);
                // If serialization succeeds, reset PreviousMessage
                // Otherwise, we received a partial message, so the following messages on this websocket should have the rest of the message

                var message = new CustomWebSocketMessage()
                {
                    MessageDateTime      = DateTime.Now,
                    Type                 = WebSocketMessageType.Text,
                    Username             = userWebSocket.UserId,
                    Message              = incomingMessage.Message,
                    WebSocketMessageType = incomingMessage.WebSocketMessageType,
                };

                byte[] output = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(message));

                await BroadcastInGroupAsync(output, userWebSocket, wsFactory);
            }
            catch (Exception ex)
            {
            }
        }