gpt4 book ai didi

c# - Signalr 无法连接远程服务器

转载 作者:行者123 更新时间:2023-11-30 22:04:26 25 4
gpt4 key购买 nike

所以,我安装了 signalr 库,除了远程连接外,一切都很好。我的客户端很容易连接到本地服务器,但是当我尝试远程连接时,出现下一个错误:无法连接远程服务器。

Firewall if off

StartUp.cs

[assembly: OwinStartup(typeof(PushNotifier.StartUp))]
namespace PushNotifier
{
public class StartUp
{
public void Configuration(IAppBuilder appBuilder)
{
appBuilder.Map("/signalr", map =>
{
var hubConfiguration = new HubConfiguration
{
EnableDetailedErrors = true,
};
map.UseCors(CorsOptions.AllowAll);
map.RunSignalR(hubConfiguration);
});
}
}
}

Program.cs

  public static void Main(string[] args)
{
try
{
using (WebApp.Start("http://*:8734"))
{
while (true)
{
var pressedKey = Console.ReadKey(true).Key;

switch (pressedKey)
{
case ConsoleKey.P:
{
var hubEntity = new HubEntity();
hubEntity.SendNotification("hidden", JsonConvert.DeserializeObject<VersionEntity>(FileHelper.OpenFile(filePath)).Version);
}
break;

case ConsoleKey.Escape:
return;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "|" + ex.StackTrace);
}
}

Client.cs

 var connection = new HubConnection("http://10.0.0.18:8734/signalr");

var hubProxy = connection.CreateHubProxy("HubEntity");

hubProxy.On<string, string>("addMessage", (message, version) =>
{
try
{
Console.WriteLine("Connected");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
});

try
{
await connection.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, string.Empty, MessageBoxButton.OK, MessageBoxImage.Error);
Application.Current.Shutdown();
}

最佳答案

我最近帮助了另一个用户,他在网络上遵循了类似或相同的示例,之所以这么说,是因为代码非常相似,方法也几乎相同。

我在远程部署服务器时尝试连接时发现的一个问题。就是这样,

var connection = new HubConnection("http://10.0.0.18:8734/signalr");

只是改成了

var connection = new HubConnection("http://10.0.0.18:8734/");

难题的下一部分可能是端口,但是基于这样一个事实,即当您浏览到该地址时,您会收到未知的传输错误,这在这种情况下很好,因此端口已打开并且通信正常。

另一个常见问题是实际的集线器名称,有些人弄错了大小写,但是为了让我们检查这一点,您需要向我们提供集线器实现,或者您可以简单地尝试阅读名称和名称在某些情况下,方法需要在信号器客户端中更改大小写。

另一个问题在于使用时 等待连接.Start();

我看不到包含此代码的函数,但如果它未标记为异步,则上述调用将同步运行并会产生一些问题,这只有在客户端和服务器位于不同的机器上时才会真正可见,当延迟开始起作用时。为了消除这种情况,我建议尝试

hubConnection.Start().Wait();

进一步帮助,很难确定你接下来要做什么,但我会假设你没有超过连接点,这就是你没有放置其余代码的原因。

我只是为了引用放置我知道针对类似示例工作的代码,该代码适用于该示例的控制台版本。

{
static void Main(string[] args)
{
Console.WriteLine("Starting client http://10.0.0.18:8734/");

var hubConnection = new HubConnection("http://10.0.0.18:8734/");
//hubConnection.TraceLevel = TraceLevels.All;
//hubConnection.TraceWriter = Console.Out;
IHubProxy myHubProxy = hubConnection.CreateHubProxy("MyHub");

myHubProxy.On<string, string>("addMessage", (name, message) => Console.Write("Recieved addMessage: " + name + ": " + message + "\n"));
myHubProxy.On("heartbeat", () => Console.Write("Recieved heartbeat \n"));
myHubProxy.On<HelloModel>("sendHelloObject", hello => Console.Write("Recieved sendHelloObject {0}, {1} \n", hello.Molly, hello.Age));

hubConnection.Start().Wait();

while (true)
{
string key = Console.ReadLine();
if (key.ToUpper() == "W")
{
myHubProxy.Invoke("addMessage", "client message", " sent from console client").ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("!!! There was an error opening the connection:{0} \n", task.Exception.GetBaseException());
}

}).Wait();
Console.WriteLine("Client Sending addMessage to server\n");
}
if (key.ToUpper() == "E")
{
myHubProxy.Invoke("Heartbeat").ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("There was an error opening the connection:{0}", task.Exception.GetBaseException());
}

}).Wait();
Console.WriteLine("client heartbeat sent to server\n");
}
if (key.ToUpper() == "R")
{
HelloModel hello = new HelloModel { Age = 10, Molly = "clientMessage" };
myHubProxy.Invoke<HelloModel>("SendHelloObject", hello).ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("There was an error opening the connection:{0}", task.Exception.GetBaseException());
}

}).Wait();
Console.WriteLine("client sendHelloObject sent to server\n");
}
if (key.ToUpper() == "C")
{
break;
}
}

}
}

关于c# - Signalr 无法连接远程服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25253926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com