我在集线器SignalR中有一个SQLDependecy方法。 我可以在Xamarin获得此回报吗? 例如,在客户端ASP.NET中我通过JavaScript获得。 我在android中需要这个依赖,如果有人和SignalR一起工作,请找我解决方案。不是通常的集线器连接,我没有找到在Android的集线器中读取SQLDependecy。
public static void Show()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.All.displayStatus();
}
public static IEnumerable<Products> GetData()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT [Guests] FROM [1]", connection))
{
// Make sure the command object does not already have
// a notification object associated with it.
command.Notification = null;
SqlDependency.Start(ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString);
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
using (var reader = command.ExecuteReader())
return reader.Cast<IDataRecord>()
.Select(x => new Products()
{
Guests = x.GetString(0),
}).ToList();
}
}
}
private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
Show();
}
$(function () {
// Proxy created on the fly
var job = $.connection.myHub;
// Declare a function on the job hub so the server can invoke it
job.client.displayStatus = function () {
getData();
};
// Start the connection
$.connection.hub.start();
getData();
});
function getData() {
var $tbl = $('#tbl');
$.ajax({
url: 'index.aspx/GetData',
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST",
success: function (data) {
debugger;
if (data.d.length > 0) {
var newdata = data.d;
$tbl.empty();
$tbl.append(' <tr><th>ID</th><th>Name</th><th>Last Executed Date</th><th>Status</th></tr>');
var rows = [];
for (var i = 0; i < newdata.length; i++) {
rows.push(' <tr><td>' + newdata[i].Guests + '</td></tr>');
}
$tbl.append(rows.join(''));
}
}
});
}
答案 0 :(得分:0)
您需要SignalR的Xamarin客户端。
https://www.nuget.org/packages/Microsoft.AspNet.SignalR.Client
如下所示,您几乎可以做到这一点。
public class SignalRClient : INotifyPropertyChanged
{
private HubConnection Connection;
private IHubProxy ChatHubProxy;
public delegate void MessageReceived(string username, string message);
public event MessageReceived OnMessageReceived;
public SignalRClient(string url)
{
Connection = new HubConnection(url);
Connection.StateChanged += (StateChange obj) => {
OnPropertyChanged("ConnectionState");
};
ChatHubProxy = Connection.CreateHubProxy("Chat");
ChatHubProxy.On<string, string>("MessageReceived", (username, text) => {
OnMessageReceived?.Invoke(username, text);
});
}
public void SendMessage(string username, string text)
{
ChatHubProxy.Invoke ("SendMessage", username, text);
}
....
您可以在此处查看完整示例https://github.com/schneidenbach/Xamarin-Forms-and-SignalR-Example。
https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/supported-platforms