请帮助我,当我尝试建立 hubConnection 时,我不断收到 System.Net.Http.HttpRequestException。我已经浏览了很多教程并下载了示例代码,但似乎没有任何效果。我也尝试过在线搜索配置计算机的方法,但很快就变得相当混乱。任何建议都将受到欢迎。
InnerException : No connection could be made because the target machine actively refused it
注意:在本地主机上一切正常
我的 asp.net c# 服务器应用程序在 IIS 上运行。它在 Live 服务器上运行。可以访问任何地方。
遵循 asp.net C# 服务器端应用程序的代码
Index.aspx 代码在这里:
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<title>SignalR Echo</title>
<script src="Scripts/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="Scripts/jquery.signalR-2.1.2.min.js"></script>
<script type="text/javascript" src="Scripts/jquery.signalR-2.1.2.js"></script>
<script src='<%: ResolveClientUrl("~/myhubs/hubs") %>'></script>
</head>
<body>
<form id="form1" runat="server">
<script type="text/javascript">
function htmlEncode(value) {
return $("<div/>").text(value).html();
}
function addMsg(msg) {
$("#messages").append("<li>" + htmlEncode(msg) + "</li>");
}
$(function () {
var chatHubProxy = $.connection.myChatHub;
chatHubProxy.client.appendNewMessage = function (clientName, message) {
addMsg(clientName + ": " + message);
};
// Start the hub connection
addMsg("Connecting Hub...");
$.connection.hub.url = "/myhubs"
$.connection.hub.logging = true;
$.connection.hub.start().done(function () {
addMsg("Server is running now.");
$("#send").click(function () {
chatHubProxy.server.broadcastMessage("Server: ", $("#msg").val());
});
}).fail(function () {
addMsg("Server is not running.");
});
});
</script>
<table>
<tr>
<td><span>Message:</span></td>
<td>
<input type="text" id="msg" />
</td>
<td>
<input type="button" value="Send" id="send" />
</td>
<td> </td>
<td />
</tr>
</table>
<ul id="messages"></ul>
</form>
</body>
</html>
Mychathub.cs 代码在这里:
using Microsoft.AspNet.SignalR;
using System;
namespace SignalRHubServer
{
/// <summary>
/// The client must use camel-cased names to RPC this Hub and its methods.
/// JS Example:
/// var chatHub = $.connection.myChatHub;
/// chatHub.server.broadcastMessage("dzy", "Hello all!");
/// </summary>
public class MyChatHub : Hub
{
public async Task BroadcastMessage(string callerName, string message)
{
// Case-insensitive when the server RPC the client's methods
await Clients.All.appendnewmessage(callerName, message);
}
}
}
startup.cs代码在这里:
using Microsoft.Owin;
using Owin;
using Microsoft.AspNet.SignalR;
[assembly: OwinStartup(typeof(SignalRHubServer.Startup))]
namespace SignalRHubServer
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR("/myhubs", new HubConfiguration());
//app.MapSignalR();
}
}
}
我的客户端正在使用控制台应用程序,并且该控制台应用程序正在另一台 PC 上运行。
这是我的 Client.cs 代码:
using Microsoft.AspNet.SignalR.Client;
using Microsoft.AspNet.SignalR.Client.Hubs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SignalRHubClient
{
class Program
{
static void Main(string[] args)
{
try
{
string inputLine;
var hubConn = new HubConnection("http://xxx.xxx.xxx.11:8080/myhubs");
var chatHubProxy = hubConn.CreateHubProxy("myChatHub");
chatHubProxy.On("appendNewMessage", delegate(string name, string message)
{
Console.WriteLine("{0}: {1}", name, message);
});
hubConn.Start().Wait();
Console.WriteLine("Success! Connected with client connection id {0}", hubConn.ConnectionId);
if (hubConn.ConnectionId != null)
{
string abd = "Tariq";
while (!string.IsNullOrEmpty(inputLine = Console.ReadLine()))
{
chatHubProxy.Invoke("broadcastMessage", abd, inputLine).Wait();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString(), "ERROR");
Console.ReadLine();
}
Console.ReadLine();
}
}
}
我在客户端控制台应用程序中遇到异常。当调用以下方法时。
注意:捕捉线上需要断点。
hubConn.Start().Wait();