我的 SignalR 项目遇到了问题。我创建了一个控制台来运行 SignalR,然后尝试在我的网站上使用它(目前都以 localhost 模式运行)
信号自主机
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
索引.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>
Run Code Online (Sandbox Code Playgroud)
上面的一些已经完成,因为我也尝试过但也没有奏效。
谁能告诉我为什么我收到错误:$.hubConnection is not a function(...)
我不确定您的 jQuery 版本是否受支持。我在加载错误版本的 jQuery 或多次加载时产生了奇怪的副作用。
使用 JSPM 时会有所jspm resolve --only npm:jquery@2.2.4帮助。
这些版本有效:
<script src="jquery-2.1.4.min.js"></script>
<script src="jquery.signalR-2.2.0.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
和
"jquery": "npm:jquery@2.2.4",
"ms-signalr-client": "npm:ms-signalr-client@2.2.5",
Run Code Online (Sandbox Code Playgroud)
我还建议在指定hubConnection. 它是自动添加的。
| 归档时间: |
|
| 查看次数: |
13946 次 |
| 最近记录: |