我正在尝试将复杂的对象从服务器传递到客户端。我的客户端代码如下所示:
var hubConnection = new HubConnection("http://somewebsite.com/");
var serverHub = hubConnection.CreateHubProxy("searchHub");
serverHub.On<Complex>("newSearch",
obj =>
{
Console.WriteLine(obj.ToString());
});
和Complex
类是:
public class Complex
{
public string url { get; set; }
public string title { get; set; }
public int results { get; set; }
public DateTime date { get; set; }
}
在服务器上,我使用:
public class SearchHub: Hub
{
public static void NewSearch(string url, string title, int results)
{
var context = GlobalHost.ConnectionManager.GetHubContext<StatsHub>();
title = Encoder.HtmlEncode(title);
url = Encoder.UrlEncode(url);
var date = DateTime.UtcNow;
context.Clients.All.newSearch(url, title, results, date);
}
}
但它不起作用。如果我从On<Complex>("newSearch"
中删除类型并将其用作.On("newSearch"
,我会获得连接,但它只打印第一个参数(url
)。我该如何从服务器传递复杂对象?