【问题标题】:Client method not called in SignalRSignalR 中未调用客户端方法
【发布时间】:2014-01-24 13:20:05
【问题描述】:

我正在使用带有 c# 的信号 R 1.0

这个问题大部分时间都在 IE(v 10) 上,有时在 chrome(v 28) 上。

我的客户端方法没有被执行。

我有聊天功能并在页面加载

$(document).ready(function(){
   //here i call server method to create group between two users
});

chat.client.groupcreated = function(){} //this is not invoked

在服务器端我写 Client.groupcreated()

它在 FF 中运行良好。更有趣的是,如果我在服务器端的 cs 代码中设置一个断点,它在 IE 中也能正常工作

【问题讨论】:

  • 如果您想使用 .net 4.0,请尝试升级到 SignalR 1.2;如果您可以使用 .net 4.5,请尝试升级到 2.0+
  • 我还会确保您附加您的客户端方法,例如在您开始 SignalR 连接之前“创建组”。
  • 是的,在我的连接开始之前,所有客户端方法都已附加

标签: c#-4.0 signalr


【解决方案1】:

Oppss......我犯了一个愚蠢的错误

在创建组的服务器方法中,我正在执行如下操作

Groups.Add(connectionid1,groupname);
Groups.Add(connectionid2,groupname);
Clients.Group(strGroupName).hellworld(); //no users added yet to the group while this line executes

上述代码的问题在于,将成员添加到信号器组实际上是异步的,因此无法立即向该组发送消息,因为向组中添加成员可能尚未完成。

解决方案

 Groups.Add(connectionid1, groupname).ContinueWith(t =>
                Groups.Add(connectionid2, groupname).ContinueWith(t2 => 
                    Clients.Group(groupname).helloworld());

在调用客户端方法之前等待任务完成

顺便说一句,感谢您发布您的 cmets

【讨论】:

    猜你喜欢
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多