我尝试在 .NET 4 下的 WebForms 应用程序中为仪表板构建通知。我已经下载了 SignalR 版本 1.2(.net 客户端和服务器)并准备了一个简单的通知示例。不幸的是它不起作用,我不明白为什么。如果我输入http://myserver.com/notificationSample/signalr/hubs http://myserver.com/notificationSample/signalr/hubsjavascript 代理出现,看起来没问题。
看看下面的实现,有人发现任何错误吗?
a) 集线器实施
[HubName("NewMessage")]
public class NewMessageNotifier : Hub
{
public void NotifyDashboards()
{
Clients.All.NewMessageCreated();
}
}
b) 通知调用者(服务器)~/Pages/NotificationCaller.aspx
public partial class NotificationCaller : Page
{
private HubConnection connection;
private IHubProxy proxy;
protected void Page_Load(object sender, EventArgs e)
{
connection = new HubConnection( "http://myserver.com/notificationSample" );
proxy = connection.CreateHubProxy( "NewMessage" );
connection.Start().Wait();
}
// it is handler for onclick event on Button control
protected void NotifyDashboard(object sender, EventArgs e)
{
proxy.Invoke( "NotifyDashboards" ).Wait();
}
}
c) 仪表板(客户端、监听器)~/Pages/Dashboard.aspx
public partial class Dashboard: BasePage
{
private HubConnection connection;
protected void Page_Load(object sender, EventArgs e)
{
connection = new HubConnection( "http://myserver.com/notificationSample" );
var proxy = connection.CreateHubProxy("NewMessage");
proxy.On("NewMessageCreated", ShowNotification);
connection.Start();
}
private void ShowNotification()
{
ShowAlert("New message added!");
}
}
你使用它的方式错误
Firstb 和 c 都是客户端,服务器自行启动,您只需添加
RouteTable.Routes.MapHubs();
to the
Application_Start
global.asax 中的方法
Second
如果您要使用网页作为客户端,您应该从 javascript 执行此操作,因为您现在所做的将不起作用,因为
connection.Start()
是异步的,请求将在执行任何操作之前结束,并且不会等待传入连接,因为所有连接都将被处理
现在该怎么办呢?这里需要很多页,所以这里有一些链接
简单教程 http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr
集线器服务器 API http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr
集线器 JavaScript API http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-javascript-client
如果你错过了,解释什么是 SignalR、它如何工作以及一个简单的应用程序的视频 http://channel9.msdn.com/Events/Build/2012/3-034你可以找到here https://github.com/SignalR/Samples
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)