我正在尝试在控制台应用程序中在 Nancy 中运行 SignalR。
当我的浏览器运行$.connection.hub.start()
时404 - NotFound
//localhost:667/negotiate?clientProtocol=1.3
----8<----
我正在(尝试)在一个端口上运行 Nancy,在另一个端口上运行 SignalR。南希与 Razor 合作。SignalR 正常返回集线器 javascript。
(很抱歉下面的代码量,但我无法进一步减少它。)
(这个问题可能从之前的一个被识别出来 - 现在删除了我标记得很糟糕的问题。)
客户端代码:
<script type="text/javascript" src='/Scripts/jquery-1.6.4.min.js'></script>
<script type="text/javascript" src="/Scripts/jquery.signalR-2.0.0-beta2.js"></script>
<script src="http://localhost:667/signalr/hubs" type="text/javascript"></script>
var chat;
$(function () {
$.connection.hub.url = '//localhost:667';
$.connection.hub.logging = true;
chat = $.connection.chat;
chat.client.addMessage = onAddMessage; // declared but not here
$.connection.hub.start()
.done(function () {
alert($.connection.id);
chat.server.send('Works!');
})
.fail(function ( failreason ) {
alert( failreason );
});
});
服务器代码(在以管理员身份运行的控制台程序中)
class Program
{
static void Main(string[] args)
{
const string webUrl = "http://localhost:666";
const string signalrUrl = "http://localhost:667";
using (var webHost = new Nancy.Hosting.Self.NancyHost(
new Uri(webUrl) ))
{
using (WebApp.Start<Startup>(signalrUrl))
{
webHost.Start();
Console.Write("Press any key");
Console.ReadKey();
webHost.Stop();
}
}
}
}
class Startup
{
public void Configuration(Owin.IAppBuilder app)
{
app.MapHubs(new HubConfiguration() { EnableCrossDomain = true });
app.UseNancy(new ApplicationBootstrapper());
}
}
public class ApplicationBootstrapper : DefaultNancyBootstrapper
{
protected override void ConfigureConventions(
Nancy.Conventions.NancyConventions nancyConventions)
{
nancyConventions.StaticContentsConventions.Add(
Nancy.Conventions.StaticContentConventionBuilder.AddDirectory(
"Scripts", @"/Scripts")
);
base.ConfigureConventions(nancyConventions);
}
}
public class Chat : Hub
{
public void Send(string message)
{
Clients.All.addMessage(message);
}
}