【发布时间】:2017-03-02 00:38:33
【问题描述】:
我的 SignalR 项目遇到了问题。我已经创建了一个控制台来运行 SignalR,然后尝试在我的网站上使用它(现在都在 localhost 模式下运行)
SignalRSelfHost
using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;
using Microsoft.AspNet.SignalR.Hubs;
namespace SignalRSelfHost
{
class Program
{
static void Main(string[] args)
{
// This will *ONLY* bind to localhost, if you want to bind to all addresses
// use http://*:8080 to bind to all addresses.
// See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx
// for more information.
string url = "http://localhost:8080";
using (WebApp.Start(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
}
}
}
class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
[HubName("myHub")]
public class MyHub : Hub
{
public void Send(string name, string message)
{
Clients.All.addMessage(name, message);
}
}
}
Index.Html
<script src="Scripts/jquery-3.1.1.min.js"></script>
<script src="Scripts/jquery.signalR-2.2.1.min.js"></script>
<script src="http://localhost:8080/signalr/hubs"></script>
<script type="text/javascript">
$(function () {
//Set the hubs URL for the connection
//$.connection.hub.url = "http://localhost:8080/signalr";
//// Declare a proxy to reference the hub.
//var chat = $.connection.myHub;
var conn = $.hubConnection("http://localhost:8080/signalr");
var hubProxy = conn.createHubProxy('myHub');
conn.start().done(function () {
});
});
</script>
上面的一些已经完成,因为我也尝试过,但也没有用。
谁能告诉我为什么会出现错误:$.hubConnection is not a function(...)
【问题讨论】:
-
您是否在客户端使用 Visual Studio 项目模板之一?
标签: signalr signalr-hub