使用SignalR时,我的Web服务器如何知道localhost的位置?(When using SignalR how does my Web Server know where localhost

编程入门 行业动态 更新时间:2024-10-11 01:19:35
使用SignalR时,我的Web服务器如何知道localhost的位置?(When using SignalR how does my Web Server know where localhost is?)

我在一个Web应用程序中使用SignalR ,该应用程序托管在与客户端无关的单独服务器上。

在代码中,我有一个连接到客户端计算机上运行的SignalR自托管服务C# )的功能,该服务器在Web应用程序上单击/执行操作(它已由客户端安装)。 如果客户端单击我的Web应用程序上的某个按钮,那么我的服务将从JavaScript代码发送数据,因为我的服务器在http:// *:8080上运行

一切似乎都很好。 我的问题是, 怎么样

我的Web应用程序正在设置服务器端JS代码中的连接来说明这一点

MyWebApp.cshtml

//Set the hubs URL for the connection $.connection.hub.url = "http://localhost:8080/signalr";

在后台(已安装)客户端计算机上运行的我的C#服务是这样说的:

MyService.cs

protected override void OnStart(string[] args) { eventLog1.WriteEntry("Service started"); //string url = "http://localhost:8080"; string url = "http://*:8080"; SignalR = WebApp.Start(url); eventLog1.WriteEntry("Server running on " + url); }

当服务器端代码说“嘿在8080寻找localhost ”时,它们甚至可能正在进行通信?

服务说“嘿,从*开始:8080 !” (我甚至不明白*的意思)

最后但并非最不重要的是,如果我取消注释当前已注释掉的服务中的行并使用它来启动服务它将无效! 为什么我的服务会在http // *:8080上启动,而不是在http:// localhost:8080上启动 ?

I am using SignalR in a web application that is hosted on a separate server that has nothing to do with the client.

In the code, I have a function that connects to a SignalR Self-Hosted Service (C#) that is running on the client's computer who is clicking / doing things on the web application (it has been installed by the client). If the client clicks a certain button on my web application, then my service will get data sent to it from the JavaScript code since my server is running on http://*:8080.

Everything appears to be working fine. My question is, how?

My web application is setting the connection in the Server Side JS Code to say this

MyWebApp.cshtml

//Set the hubs URL for the connection $.connection.hub.url = "http://localhost:8080/signalr";

My C# Service running on the client's computer in the background (that has been installed) is saying this:

MyService.cs

protected override void OnStart(string[] args) { eventLog1.WriteEntry("Service started"); //string url = "http://localhost:8080"; string url = "http://*:8080"; SignalR = WebApp.Start(url); eventLog1.WriteEntry("Server running on " + url); }

How is it possible that they are even communicating when the server side code is saying "Hey look for localhost on 8080!"

And the service says "Hey start on *:8080!" ( I don't even understand what * means)

Last but not least, if I uncomment the line in my service that is currently commented out and use that to start the service it will not work! Why will my service start up on http//*:8080 but not on http://localhost:8080 ?

最满意答案

这段代码:

WebApp.Start("http://*:8080")

“在可用的任何IP地址上侦听端口8080上的连接” 。 因此它将绑定到localhost (127.0.0.1)以及该机器具有的任何其他分配的IP地址。 它基本上是一个通配符绑定。

这段代码:

$.connection.hub.url = "http://localhost:8080/signalr";

说服务器在localhost上,解析为127.0.0.1。

关于为什么如果服务器在http://localhost:8080上启动它不起作用的最后一点,我认为这是因为SignalR无法映射到正确的IP地址。 请尝试使用http://127.0.0.1:8080 。

This code:

WebApp.Start("http://*:8080")

Says "listen for connections on port 8080 on any IP address available". So it will bind to localhost (127.0.0.1) and any other assigned IP addresses the machine has. It is basically a wildcard binding.

This code:

$.connection.hub.url = "http://localhost:8080/signalr";

Says the server is on localhost which resolves to 127.0.0.1.

For your last point as to why it doesn't work if the server started on http://localhost:8080, I assume this is because SignalR is having trouble mapping to the correct IP address. Try using http://127.0.0.1:8080 instead.

更多推荐

本文发布于:2023-07-15 07:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1111430.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位置   服务器   Web   SignalR   localhost

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!