我正在尝试制作能够连接到“http://localhost:8080”或http://127.0.0.1:8080/的SignalR服务器和客户端架构但我无法连接我的本地 IP 地址,如“192.x.x.x”,这可能是什么原因? 请帮助我,我也将我的代码放在这里......
public partial class WinFormsServer : Form
{
private IDisposable SignalR { get; set; }
const string ServerURI = "http://localhost:8080";
private void ButtonStart_Click(object sender, EventArgs e)
{
WriteToConsole("Starting server...");
ButtonStart.Enabled = false;
Task.Run(() => StartServer());
}
private void StartServer()
{
try
{
SignalR = WebApp.Start(ServerURI);
}
catch (TargetInvocationException)
{
WriteToConsole("Server failed to start. A server is already running on " + ServerURI);
//Re-enable button to let user try to start server again
this.Invoke((Action)(() => ButtonStart.Enabled = true));
return;
}
this.Invoke((Action)(() => ButtonStop.Enabled = true));
WriteToConsole("Server started at " + ServerURI);
}
class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
}
最佳答案
我尝试了不同的解决方案,但找不到正确的解决方案。
最后我发现了只与权限有关的问题。 以管理员身份运行您的 SignalR 服务器应用程序。它将开始在本地 IP 上运行,例如 192.168.X.X:9090,然后您的客户端应用程序可以使用此 IP 地址从任何其他 PC 连接此服务器。
class Program
{
static void Main(string[] args)
{
var url = $"http://{GetLocalIPAddress()}:8080";
using (WebApp.Start<Startup>(url))
{
Console.WriteLine($"Server running at {{{url}}}");
Console.ReadLine();
}
}
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("Local IP Address Not Found!");
}
}
关于asp.net-mvc - SignalR:无法连接到本地或任何其他 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38100618/