如何从另一个网页上的页面点击事件调用signalr hub方法。 [英] How to call signalr hub method from a page click event on another webpage.
问题描述
我希望添加推送通知,只有在按钮的点击事件中调用特定功能时才会将状态消息推送到客户端浏览器。接收信号器发送消息的客户端javascript方法位于PageA.aspx中,如下所示。
< script src =Scripts / jquery-1.6.4 .js>< / script>
< script src =Scripts / jquery.signalR-2.2.0.js>< / script>
< script type =text / javascript>
$(function(){
var connection = $ .hubConnection();
var hub = connection.createHubProxy(hitCounter);
hub.on(onRecordHit,function(hitCount){
$('#hitCountValue') .text(hitCount);
});
connection.start(function(){
hub .invoke('recordHit');
});
});
< / script>
Hub代码如下所示
I am looking to add push notifications which will push status messages to client browser only when specific functions are called within a button's click event. The client javascript methods which will accept signalr sent messages are located in PageA.aspx which looks like this.
<script src="Scripts/jquery-1.6.4.js"></script>
<script src="Scripts/jquery.signalR-2.2.0.js"></script>
<script type="text/javascript" >
$(function () {
var connection = $.hubConnection();
var hub = connection.createHubProxy("hitCounter");
hub.on("onRecordHit", function (hitCount) {
$('#hitCountValue').text(hitCount);
});
connection.start(function () {
hub.invoke('recordHit');
});
});
</script>
The Hub Code looks like this
//[HubName("hitCounter")]
public class HitCounterHub: Hub
{
static int _hitcount = 0;
public void RecordHit()
{
_hitcount += 1;
Clients.All.onRecordHit(_hitcount);
}
}
使用此示例在浏览器上触发的代码直到我试图从另一个网页(PageB.aspx)调用RecordHit()方法通过在PageB的Button1_Click事件上执行以下操作。
The code fired on the browser with this example up until I attempted to call the RecordHit() Method from another webpage(PageB.aspx) By doing the following on PageB's Button1_Click event.
var myhubContext = GlobalHost.ConnectionManager.GetHubContext<HitCounterHub>();
myhubContext.Clients.All.RecordHit();
我在这里缺少什么?
Is there something I am missing here?
推荐答案
(function(){
var connection =
(function () {
var connection =
.hubConnection();
var hub = connection.createHubProxy(hitCounter);
hub.on(onRecordHit,function(hitCount){
.hubConnection();
var hub = connection.createHubProxy("hitCounter");
hub.on("onRecordHit", function (hitCount) {
('#hitCountValue')。text(hitCount);
});
connection.start(function(){
hub.invoke('recordHit');
});
});
< / script>
Hub代码如下所示
('#hitCountValue').text(hitCount);
});
connection.start(function () {
hub.invoke('recordHit');
});
});
</script>
The Hub Code looks like this
//[HubName("hitCounter")]
public class HitCounterHub: Hub
{
static int _hitcount = 0;
public void RecordHit()
{
_hitcount += 1;
Clients.All.onRecordHit(_hitcount);
}
}
使用此示例在浏览器上触发的代码直到我试图从另一个网页(PageB.aspx)调用RecordHit()方法通过在PageB的Button1_Click事件上执行以下操作。
The code fired on the browser with this example up until I attempted to call the RecordHit() Method from another webpage(PageB.aspx) By doing the following on PageB's Button1_Click event.
var myhubContext = GlobalHost.ConnectionManager.GetHubContext<HitCounterHub>();
myhubContext.Clients.All.RecordHit();
我在这里缺少什么?
Is there something I am missing here?
这篇关于如何从另一个网页上的页面点击事件调用signalr hub方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!