Пример #1
0
        public async Task Send(string userName, string message)
        {
            await HubConnection.InvokeAsync("SendMessage", userName, message);

            MessageDelegateArgs args = new MessageDelegateArgs()
            {
                UserName = userName,
                Message  = message,
            };

            OnSendMessage(args);
        }
Пример #2
0
        public static async Task <Connection> Create(HttpAuthorizationResult authorization)
        {
            HubConnection hub = new HubConnectionBuilder().WithUrl(Routes.SignalR.Connection, options =>
            {
                options.AccessTokenProvider = () => Task.FromResult(authorization.Token);
                options.Headers.Add("Id", authorization.Id);
                options.Headers.Add("UserName", authorization.User);
            }).AddMessagePackProtocol().Build();

            Connection connection = new Connection(authorization.User)
            {
                Friends = await HttpRestClient.GetFriendsWithStatus(authorization.Id)
            };


            hub.On("NotificationFriendChangeStatus", async(string userName, bool state) =>
            {
                try
                {
                    var isUserExist = connection.Friends.ContainsKey(userName);
                    if (isUserExist)
                    {
                        connection.Friends[userName] = state;
                        var args = new ChangeStatusFriendDelegateArgs()
                        {
                            Status   = state,
                            UserName = userName,
                        };
                        connection.OnChangeStatusFriend(args);
                    }
                    else
                    {
                        await Log.WriteAsync($"Error:Nie znaleziono użytkownika:{userName} którego status miał zostać zmieniony");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            });

            hub.On("IncomingCall", async(string userName, string connectionId) =>
            {
                Console.WriteLine("Połączenie przychodzące");
                connection.OnIncomingCall(new ConnectionDelegateArgs()
                {
                    User = userName
                });
            });

            hub.On("CallAccepted", async(string userName) =>
            {
                Console.WriteLine("Połączenie zaakceptowane");
                connection.Session.UserNameFriend = userName;
                connection.Session.CreateOfferConnection();
            });

            hub.On("CallDeclined", async(string userName) =>
            {
                Console.WriteLine("Połączenie odrzucono");
            });

            hub.On("ReceivingMessagesAsync", (string username, string message) =>
            {
                var args = new MessageDelegateArgs()
                {
                    UserName = username,
                    Message  = message,
                };

                connection.OnReceiveMessage(args);
            });

            hub.On("SdpMessageReceivedConfigurationWebRtc", async(string userName, SdpMessage sdpMessage) =>
            {
                await connection.Session.SetRemoteDescriptionAsync(sdpMessage);
                connection.Session.CreateAnswerConnection();
                Console.WriteLine("Odebranie konfiguracji SDP");
            });

            hub.On("IceCandidateReceivedConfigurationWebRtc", (string userName, IceCandidate iceCandidate) =>
            {
                connection.Session.AddIceCandidate(iceCandidate);
                Console.WriteLine("Odebranie konfiguracji ICE");
            });

            await hub.StartAsync();

            connection.HubConnection = hub;
            return(connection);
        }
Пример #3
0
 static void ReceiveMessage(object sender, MessageDelegateArgs args)
 {
     Console.WriteLine(args.UserName + ": " + args.Message);
 }
Пример #4
0
 public void OnReceiveMessage(MessageDelegateArgs args)
 {
     ReceiveMessage?.Invoke(this, args);
 }
Пример #5
0
 public void OnSendMessage(MessageDelegateArgs args)
 {
     SendMessage?.Invoke(this, args);
 }