[英]Calling SignalR Hub method from client locks Xamarin android App
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. 我终于有一个SignalR Hub使用Microsoft.AspNet.SignalR副Microsoft.AspNetCore.SignalR,我无法获得Microsoft.AspNetCore.SignalR,不明白为什么。 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. 我能够连接,使用OnConnect将客户端链接到连接ID,并使用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();
一切正常,除非我调用GetConnectedUsers()
,当我从客户端使用此代码调用它时, 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);
}
}
}
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: 感谢David Fowler在GitHub上提供了本文档的链接( https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md#avoid-using-taskresult-and-taskwait ),我能够通过更改我的代码来实现此目的,如下所示:
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: Client.cs中对ConnectedUsers函数的调用也发生了变化:来自:
List<string> userList = client.ConnectedUsers();
To: 至:
List<string> userList = await client.ConnectedUsers();
No changes to the hub code were necessary. 不需要更改集线器代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.