我使用SignalR與ASP.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' 方法無法解析。
同樣,我想要做的是做一個簡單的測試來檢查如何從客戶端和客戶端從服務器調用服務器。
謝謝。