嘿伙计们,我是第一次尝试 siqnalr,但在让它工作时遇到了一些问题。
我可以让客户毫无问题地调用集线器方法。我可以让集线器毫无问题地给客户打电话。但我想从我的服务器端按钮单击事件调用集线器,以便我可以使用处理信息更新客户端,但无论我尝试什么,我都无法让它工作。
有人有什么想法吗?
我可能错过了一些简单的事情,但我不知道出了什么问题。
这是我的代码:
全球.asax
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHubs();
}
测试集线器.cs
using System;
using System.Linq;
using Microsoft.AspNet.SignalR;
namespace Hubs
{
/// <summary>
/// Summary description for TestHub
/// </summary>
public class TestHub : Hub
{
public void Send(string name, string message)
{
// Call the broadcastMessage method to update clients.
Clients.Caller.broadcastMessage(name, message);
}
internal static void Send2(string name, string message)
{
//test 1
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<TestHub>();
context.Clients.All.Send(name, message);
//test 2
IHubContext context2 = GlobalHost.ConnectionManager.GetHubContext<TestHub>();
context2.Clients.All.broadcastMessage(name, message);
}
}
}
默认.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="Signalr2._default" %>
<!DOCTYPE html>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
<Scripts>
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
<asp:ScriptReference Path="~/Scripts/jquery.signalR-1.1.2.js"/>
<asp:ScriptReference Path="~/signalr/hubs" />
</Scripts>
</telerik:RadScriptManager>
<telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
<script type="text/javascript">
$(function ()
{
// Declare a proxy to reference the hub.
var ifiHub = $.connection.testHub;
// Create a function that the hub can call to broadcast messages.
ifiHub.client.broadcastMessage = function (name, message)
{
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName + '</strong>: ' + encodedMsg + '</li>');
};
//Start the connection.
$.connection.hub.start().done(function ()
{
ifiHub.server.send('connection started:', $.connection.hub.id);
});
});
</script>
</telerik:RadScriptBlock>
<asp:Button ID="Button1" runat="server" Text="Server To Client Test" OnClick="Button1_Click" />
<br /><br />
<ul id="discussion"/>
</form>
</body>
</html>
默认.aspx.cs
using System;
using System.Linq;
using Hubs;
using Microsoft.AspNet.SignalR;
namespace Signalr2
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//test 1
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<TestHub>();
context.Clients.All.broadcastMessage("Now", DateTime.Now.ToString());
//test 2
IHubContext context2 = GlobalHost.ConnectionManager.GetHubContext<TestHub>();
context2.Clients.All.Send("Now", DateTime.Now.ToString());
//test 3
TestHub.Send2("Now", DateTime.Now.ToString());
string test = "";
}
}
}