Exemplo n.º 1
0
        public async static Task SendMessages(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post")] object message,
            [SignalR(HubName = "chat")] IAsyncCollector <SignalRMessage> signalRMessages)
        {
            var jsonObject = (JObject)message;
            var msg        = jsonObject.ToObject <Message>();

            if (msg.TypeInfo.Name == nameof(PhotoMessage))
            {
                var photoMessage = jsonObject.ToObject <PhotoMessage>();

                var bytes = Convert.FromBase64String(photoMessage.Base64Photo);

                var stream          = new MemoryStream(bytes);
                var subscriptionKey = Environment.GetEnvironmentVariable("ComputerVisionKey");
                var computerVision  = new ComputerVisionClient(new ApiKeyServiceClientCredentials(subscriptionKey), new DelegatingHandler[] { });

                computerVision.Endpoint = Environment.GetEnvironmentVariable("ComputerVisionEndpoint");

                var features = new List <VisualFeatureTypes>()
                {
                    VisualFeatureTypes.Adult
                };

                var result = await computerVision.AnalyzeImageInStreamAsync(stream, features);

                if (result.Adult.IsAdultContent)
                {
                    return;
                }

                var url = await StorageHelper.Upload(bytes, photoMessage.FileEnding);

                msg = new PhotoUrlMessage(photoMessage.Username)
                {
                    Id        = photoMessage.Id,
                    Timestamp = photoMessage.Timestamp,
                    Url       = url
                };

                await signalRMessages.AddAsync(new SignalRMessage
                {
                    Target    = "newMessage",
                    Arguments = new[] { message }
                });

                return;
            }

            await signalRMessages.AddAsync(new SignalRMessage
            {
                Target    = "newMessage",
                Arguments = new[] { message }
            });
        }
Exemplo n.º 2
0
        public override async void Init(object initData)
        {
            base.Init(initData);

            var data = initData as Tuple <Greeting, string>;

            UserName  = data.Item1.Username;
            FileName  = data.Item1.FileName;
            GroupName = data.Item2;

            var messageGreeting = new PhotoUrlMessage(UserName);

            if (!string.IsNullOrEmpty(FileName))
            {
                messageGreeting.Url = FileName;
                var message = new UserConnectedMessage(UserName, GroupName);
                await ChatService.JoinChannelAsync(message);

                Messages.Add(messageGreeting);
            }
            var user = await ChatService.GetUsersGroup(GroupName);

            Users = new ObservableCollection <User>(user);
        }
Exemplo n.º 3
0
        public static async Task Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req,
            [SignalR(HubName = "chat")] IAsyncCollector <SignalRMessage> signalRMessages,
            ILogger log)
        {
            var serializedObject = new JsonSerializer().Deserialize(new JsonTextReader(new StreamReader(req.Body)));

            var message = JsonConvert.DeserializeObject <ChatMessage>(serializedObject.ToString());

            if (message.TypeInfo.Name == nameof(UserConnectedMessage))
            {
                message = JsonConvert.DeserializeObject <UserConnectedMessage>(serializedObject.ToString());
                await signalRMessages.AddAsync(
                    new SignalRMessage
                {
                    GroupName = message.GroupName,
                    Target    = "ReceiveMessage",
                    Arguments = new[] { message }
                });
            }
            else if (message.TypeInfo.Name == nameof(SimpleTextMessage))
            {
                message = JsonConvert.DeserializeObject <SimpleTextMessage>(serializedObject.ToString());
                var signalRMessage = new SignalRMessage
                {
                    Target    = "ReceiveMessage",
                    Arguments = new[] { message }
                };

                if (message.GroupName != null)
                {
                    signalRMessage.GroupName = message.GroupName;
                }
                else if (message.Recipient != null)
                {
                    signalRMessage.UserId = message.Recipient;
                }

                await signalRMessages.AddAsync(signalRMessage);
            }
            else if (message.TypeInfo.Name == nameof(PhotoMessage))
            {
                var photoMessage = JsonConvert.DeserializeObject <PhotoMessage>(serializedObject.ToString());

                var bytes = Convert.FromBase64String(photoMessage.Base64Photo);
                var url   = await StorageHelper.Upload(bytes, photoMessage.FileEnding);

                message = new PhotoUrlMessage(photoMessage.Sender)
                {
                    Id        = photoMessage.Id,
                    Timestamp = photoMessage.Timestamp,
                    Url       = url,
                    GroupName = photoMessage.GroupName
                };

                await signalRMessages.AddAsync(
                    new SignalRMessage
                {
                    GroupName = message.GroupName,
                    Target    = "ReceiveMessage",
                    Arguments = new[] { message }
                });
            }
        }
Exemplo n.º 4
0
        public static async Task Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req,
            // Establecemos el hub de conexión
            [SignalR(HubName = "chat")] IAsyncCollector <SignalRMessage> signalRMessages,
            ILogger log)
        {
            // Se obtiene el contenido del Request Body
            var serializeObject = new JsonSerializer().Deserialize(new JsonTextReader(new StreamReader(req.Body)));

            // Se deserializa el mensaje obtenido del body
            var message = JsonConvert.DeserializeObject <ChatMessage>(serializeObject.ToString());

            // Verificamos que tipo de mensaje es acorde a la clase y asi poder procesarlo
            if (message.TypeInfo.Name == nameof(UserConnectedMessage))
            {
                // Se deserializa el tipo obtenido
                message = JsonConvert.DeserializeObject <UserConnectedMessage>(serializeObject.ToString());

                // Se agrega la funcion de signalR
                await signalRMessages.AddAsync(new SignalRMessage
                {
                    GroupName = message.GroupName,
                    Target    = "ReceiveMessage",
                    Arguments = new[] { message }
                });
            }
            else if (message.TypeInfo.Name == nameof(SimpleTextMessage))
            {
                // Se deserializa el tipo obtenido
                message = JsonConvert.DeserializeObject <SimpleTextMessage>(serializeObject.ToString());

                var signalRMessage = new SignalRMessage
                {
                    // Nombre del metodo que agregara el mensaje al chat
                    Target = "ReceiveMessage",
                    // Es el mensaje a enviar por Signal R
                    Arguments = new[] { message }
                };
                // Unicamente se pueden configurar estas propiedades una a la vez
                if (message.GroupName != null)
                {
                    signalRMessage.GroupName = message.GroupName;
                }
                else if (message.Recipient != null)
                {
                    signalRMessage.UserId = message.Recipient;
                }

                // Se agrega a la función a signalR
                await signalRMessages.AddAsync(signalRMessage);
            }
            else if (message.TypeInfo.Name == nameof(PhotoMessage))
            {
                // Se deserializa el tipo obtenido
                var photoMessage = JsonConvert.DeserializeObject <PhotoMessage>(serializeObject.ToString());

                // se convierte a bytes la propiedad de Photo
                var bytes = Convert.FromBase64String(photoMessage.Base64Photo);

                var url = await StorageHelper.Upload(bytes, photoMessage.FileEnding);

                // Se establecen las propiedades del mesaje
                message = new PhotoUrlMessage(photoMessage.Sender)
                {
                    Id        = photoMessage.Id,
                    Timestamp = photoMessage.Timestamp,
                    Url       = url,
                    GroupName = photoMessage.GroupName
                };

                // Se agrega a la función de signalR
                await signalRMessages.AddAsync(new SignalRMessage {
                    GroupName = message.GroupName,
                    Target    = "ReceiveMessage",
                    Arguments = new[] { message }
                });
            }
        }