0

I finally got a SignalR Hub to work using the Microsoft.AspNet.SignalR vice the Microsoft.AspNetCore.SignalR, I was unable to get the Microsoft.AspNetCore.SignalR, no idea why. But I did get the other one to work. I am able to connect, link clients to connection id's using OnConnect and removing them using OnDisconnect. My Hub code is:

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.SignalR;
using SignalrHub;

namespace SignalRChat
{
    public class ChatHub : Hub
    {

        private static readonly List<User> Users = new List<User>();

        public override Task OnConnected()
        {

            // string userName = Context.User.Identity.Name;
            string userName = Context.QueryString["username"];
            string org= Context.QueryString["organization"];
            string dept = Context.QueryString["dept"];
            string team = Context.QueryString["team"];
            string firstname = Context.QueryString["firstname"];
            string lastname = Context.QueryString["lastname"];
            string connectionId = this.Context.ConnectionId;

            // for now I just capture username and connection Id
            var user = new User(); 
            user.Name = userName;
            user.ConnectionIds = connectionId;


            try
            {
                Users.Add(user);
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
            }

            // TODO: Broadcast the connected user

            // send list of connected users to client
            Send("Welcome " + userName, "Connected users are:");

            foreach (var display in Users)
            {
                Send("",display.Name.ToString());

            }


            return base.OnConnected();
        }

        public override Task OnDisconnected(bool stopped)
        {

            string userName = Context.User.Identity.Name;
            string connectionId = Context.ConnectionId;

            var item = Users.Find(x => x.ConnectionIds == connectionId);
            Users.Remove(item);

            return base.OnDisconnected(true);
        }


        public void Send(string name, string message)
        {
            // Call the broadcastMessage method to update clients.
            Clients.All.broadcastMessage(name, message);
        }

        public List<String> GetConnectedUsers()
        {
            List<string> UserNames = new List<string>();

            foreach (var ConnectedUser in Users)
            {
                UserNames.Add(ConnectedUser.Name);
            }

            return UserNames;
        }
    }
}

Everything works fine except when I call GetConnectedUsers(), when I call that from the client with this code ConnecteduserList = client.ConnectedUsers(); the app locks up, eg; the hub never returns from that method. Clearly I'm missing something. Can anyone tell me what?

The client code in the app is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client;

namespace ChatClient.Shared
{
    class Client
    {
        //public string username;
        private readonly string _platform;
        private readonly HubConnection _connection;
        private readonly IHubProxy _proxy;

        public event EventHandler<string> OnMessageReceived;

        public Client(string platform, string username)
        {
            string _username = "username=" + username;
            _platform = platform;
            _connection = new HubConnection("https://MyApp.com/SignalRhub", _username);
            _proxy = _connection.CreateHubProxy("chathub");
        }

        public async Task Connect()
        {

            await _connection.Start();                 _proxy.On("broadcastMessage", (string platform, string message) =>
            {
                if (OnMessageReceived != null)
                    OnMessageReceived(this, string.Format("{0}: {1}", platform, message));
            });

            Send("Connected");
        }

        public List<string> ConnectedUsers()
        {
            List<string> Users = new List<string>();

            // Locks up when this line is esecuted.  The server log has nothing in it.
            Users =  _proxy.Invoke<List<string>>("GetConnectedUsers").Result;

            return Users;
        }

        public Task Send(string message)
        {
            return _proxy.Invoke("Send", _platform, message);
        }
    }
}
Prescott Chartier
  • 1,519
  • 3
  • 17
  • 34
  • Did you use the fiddler in the mobile phone and servers ? To capture network package.If you can send to package to the servers and get the package in the mobile phone. – Leon Apr 05 '19 at 06:01
  • I'm not familiar with fiddler, what is it and how do I get it? – Prescott Chartier Apr 05 '19 at 17:09
  • You can refer to this link about how to use it https://zappysys.com/blog/how-to-use-fiddler-to-analyze-http-web-requests/ If you want to capture network packages in mobile phone, this link could help you https://docs.telerik.com/fiddler/Configure-Fiddler/Tasks/ConfigureForAndroid – Leon Apr 08 '19 at 01:36

1 Answers1

0

Thanks to David Fowler over at GitHub who provided the link to this document (https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md#avoid-using-taskresult-and-taskwait), I was able to get this to work by changing my code as follows:

On the client: From:

public List<string> ConnectedUsers()
{
    // Hangs on this line
    List<string> Users = _proxy.Invoke<List<string>>("getConnectedUsers").Result;

    return Users;
}

To:

public async Task <List<string>> ConnectedUsers()
{
    List<string> Users = await _proxy.Invoke<List<string>>("getConnectedUsers");
    return Users;
}

The call to the ConnectedUsers function in Client.cs was changed as well: From:

List<string> userList = client.ConnectedUsers();

To:

List<string> userList = await client.ConnectedUsers();

No changes to the hub code were necessary.

Prescott Chartier
  • 1,519
  • 3
  • 17
  • 34