2012-09-29 36 views
0

我使用SignalRASP.NET 4.5 web表单。我无法让客户与服务器通话,反之亦然。我想要实现的只是能够测试客户端如何触发服务器功能以及服务器如何触发客户端功能。下面的代码我使用:SignalR错误在ASP.NET 4.5网站调用来自客户端服务器时,500

客户端代码(HitCounter.aspx)

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title></title> 
<script type="text/javascript" src="Scripts/jquery-1.8.2.js"></script> 
<script src="Scripts/jquery.signalR-0.5.3.min.js"></script> 
<script type="text/javascript" src="SignalR/Hubs"></script> 

<style> 
    #currentHitCount { 
     font-family: Arial; 
     font-size:40pt; 
     margin-left:auto; 
     margin-right:auto; 
     display:block; 
     text-align:center; 
    } 

</style> 
</head> 
<body> 
<div id="currentHitCount"></div> 
<script type="text/javascript"> 

    $(function() { 
     var hub = $.connection.hitCounter; 

     $.extend(hub, { 
      showHitCount: function (hitCount){ 
       if(hitCount > 1){ 
        $('#currentHitCount') 
        .html("This site has had " + hitCount + " hits."); 
       } 
       else{ 
        $('#currentHitCount') 
        .html("This site has had " + hitCount + " hit."); 
       } 
      }, 
      addMessage: function (str) { 
       $('#currentHitCount') 
       .html("Getting Message: " + str); 
      } 
     }); 

     $.connection.hub.start(function() { 
      hub.addMessage("test"); 
      hub.addHit(); 

     }); 

     $.connection.hub.stateChanged(function (change) { 
      if ($.signalR.connectionState["connected"] === change.newState) {     
      } 
     }); 

     $.connection.hub.error(function() { 
     }); 

    }); 


    </script> 
    <div style="background-color:red; width:290px; height:200px; color:white; position:absolute; top:100px; left:30px;" id="thebutton">test</div> 

</body> 
</html> 

服务器端代码(App_Code文件/ HitCounterHub.cs)

using SignalR.Hubs; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using System.Web; 

[HubName("hitCounter")] 
public class HitCounterHub : Hub, IDisconnect 
{ 
    static int _hitCount; 

public void addHit(string pageId) 
{ 
    _hitCount += 1; 
    Clients.showHitCount(_hitCount); 

} 

public Task ClientCallingServer() 
{ 
    _hitCount += 1; 
    return Clients["foo"].showHitCount(_hitCount); 
} 


public Task Join() 
{ 
    return Groups.Add(Context.ConnectionId, "foo"); 
} 

public Task Send(string message) 
{ 
    _hitCount += 1; 
    return Clients["foo"].addMessage(message);  
} 


public Task Disconnect() 
{ 
    return Clients["foo"].leave(Context.ConnectionId); 
} 
} 

我运行代码本地在IIS7.5上,但是在Visual Studio 2012中运行代码时出现错误。

的错误:

localhost/signalr/signalr/send?transport=serverSentEvents&connectionId=400f1302-6c8e-418e-a14c-da95f836a29d 500 (Internal Server Error)

使用Chrome调试器的错误页面显示: 'addHit' 方法无法解析。

同样,我想要做的是做一个简单的测试来检查如何从客户端和客户端从服务器调用服务器。

谢谢。

回答

1

您无法解决您的addHit方法的原因是因为您在服务器上的功能名称与客户端的名称相同。

AKA要调用你正在做的服务器hub.addHit(“laksjdf”)。但要调用客户端,它会是同样的事情:hub.addHit(“laksjdfldksj”);因此有歧义。更改您的客户端或服务器端功能的名称,使其在命名意义上是唯一的。

此问题将在SignalR的下一个版本中修复。

相关问题