我可以让客户端调用hub方法,没有任何问题。 我可以让集线器无故障地呼叫客户端。 但我想从我的服务器端按钮单击事件调用集线器,这样我就可以使用处理信息更新客户端,但无论我尝试什么,我都无法让它工作。
有没有人有任何想法?
我可能错过了一些简单的事情,但我不知道出了什么问题。
这是我的代码:
Global.asax中
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHubs();
}
TestHub.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);
}
}
}
的Default.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>
default.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 = "";
}
}
}
答案 0 :(得分:0)
从客户端调用集线器的方法需要公开尝试从Send2内部切换到公共 -