从客户端锁定Xamarin android App调用SignalR Hub方法

问题描述 投票:0回答:1

我终于有一个SignalR Hub使用Microsoft.AspNet.SignalR副Microsoft.AspNetCore.SignalR,我无法获得Microsoft.AspNetCore.SignalR,不明白为什么。但我确实得到了另一个工作。我能够连接,使用OnConnect将客户端链接到连接ID,并使用OnDisconnect删除它们。我的中心代码是:

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;
        }
    }
}

一切正常,除非我打电话给GetConnectedUsers(),当我从客户端使用此代码ConnecteduserList = client.ConnectedUsers();调用该应用程序锁定时,例如;集线器永远不会从该方法返回。显然我错过了一些东西。谁能告诉我什么?

应用中的客户端代码是:

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);
        }
    }
}
xamarin.android visual-studio-2017 signalr-2
1个回答
0
投票

感谢David Fowler在GitHub上提供了本文档的链接(https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md#avoid-using-taskresult-and-taskwait),我通过更改我的代码得到了以下工作:

在客户端:来自:

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

    return Users;
}

至:

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

Client.cs中对ConnectedUsers函数的调用也发生了变化:来自:

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

至:

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

不需要更改集线器代码。

© www.soinside.com 2019 - 2024. All rights reserved.