问题
I've been trying to get a javascript signal-r client working against a self-hosted owin server. And am running into this issue. I've tried both proxies and no proxy methods both with the same result of the and error "SignalR: Error during negotiation request: undefined".
I've been able to get the cross-domain sample to run without any issues and cannot figure out what I am doing wrong, anyone have any ideas?
The browser console gets logs an attempt to negotiate then I get the failure.
Server/Hub
namespace SignalROwinHost
{
class Program
{
static void Main(string[] args)
{
string url = "http://localhost:9000/";
using (WebApplication.Start<Startup>(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
}
}
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapHubs();
}
}
public class ChatHub : Hub
{
public void Send(string message)
{
Clients.All.send(message);
}
}
}
}
Javascript(no-proxy):
<script type="text/javascript">
$(function () {
var connection = $.hubConnection('http://localhost:9000');
var chat = connection.createHubProxy('chatHub');
chat.on('send', function (message) {
$('#chat').html($('#chat').html() + "\r\n" + message);
});
connection.logging = true;
connection.start().done(function () {
alert("Connection Complete");
$('#sendBtn').click(function () {
chat.invoke('send', $('#message').val());
});
}).fail(function (param) { alert(param); });
});
</script>
Javascript (proxy):
<script src="http://localhost:9000/signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var chat = $.connection.chatHub;
chat.client.send = function (message) {
alert(message);
};
$.connection.hub.logging = true;
$.connection.hub.url = "http://localhost:9000/signalr";
$.connection.hub.start()
.done(function (param) {
chat.server.send("Connected");
})
.fail(function (param) {
alert("Could not Connect: " + param);
});
});
</script>
回答1:
If you are using SignalR 1.0.0 (which you should since it is now officially released), you need to change your call to MapHubs
to enable cross-domain requests. This is assuming your website is not being served from localhost:9000. I'm guessing it isn't since you are passing in the url to $.hubConnection
.
app.MapHubs(new HubConfiguration { EnableCrossDomain = true });
Before the final release of 1.0.0, CORS was enabled by default.
来源:https://stackoverflow.com/questions/14990789/signalr-error-during-negotiation-request-undefined