问题
I'm using SignalR with ASP.NET 4.5 webforms. I wasn't able to make the client talk to the server and vice versa. What I want to achieve is simply being able to to test how the Client can trigger a server function and how the server can trigger a client function. Here's the code I use:
Client Side Code (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>
Server Side Code (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);
}
}
I am running the code locally on IIS7.5 but I get and error when running the code in Visual Studio 2012 too.
The Error:
localhost/signalr/signalr/send?transport=serverSentEvents&connectionId=400f1302-6c8e-418e-a14c-da95f836a29d 500 (Internal Server Error)
Using Chrome debugger the error page shows: 'addHit' method could not be resolved.
Again, what I am trying to do is to make a simple test to check how to call a server from the client and the client from the server.
Thanks.
回答1:
The reason why you're not able to resolve your addHit method is because you have the same function name on the server as you do the client.
AKA To call the server you're doing hub.addHit("laksjdf"). But to call the client it'd be the same thing: hub.addHit("laksjdfldksj"); Hence there's ambiguity. Change the name of either your client or server side functions so they're unique in their naming sense.
This issue will be fixed in the next release of SignalR.
来源:https://stackoverflow.com/questions/12649472/signalr-error-500-when-calling-server-from-client-in-asp-net-4-5-website