Javascript Vert.x EventBus客户端SockJS获取主机和连接端口

时间:2021-01-12 18:06:12

I am using Vert.x Core 3.3.3 and Web 3.3.3 in Java as an EventBus server and Vert.x 2.0.0 and SockJS 1.1.1 on a web client in Javascript to connect to the EventBus. I am connected, able to send and receive messages, and all inbound and outbound traffic is open. Everything is functioning properly.

我在Java中使用Vert.x Core 3.3.3和Web 3.3.3作为EventBus服务器,在Javascript中的Web客户端上使用Vert.x 2.0.0和SockJS 1.1.1连接到EventBus。我已连接,能够发送和接收消息,并且所有入站和出站流量都已打开。一切都运转正常。

My server is able to get the host and port for each EventBus connection when the client connects. This comes from the SockJSHandler's bridge event in Java, via socket().remoteAddress() in the format "host:port".

当客户端连接时,我的服务器能够获取每个EventBus连接的主机和端口。这来自Java中的SockJSHandler的桥接事件,通过socket()。remoteAddress(),格式为“host:port”。

On the web client side, is there functionality to get the remote address (host and port of the server) of the EventBus SockJS connection to my Vert.x Server's EventBus? I have looked in the documentation for Vert.x Web Client (http://vertx.io/docs/jsdoc/ & https://github.com/vert-x3/vertx-web) but have not found anything useful.

在Web客户端,是否有功能将EventBus SockJS连接的远程地址(服务器的主机和端口)连接到我的Vert.x服务器的EventBus?我查看了Vert.x Web Client的文档(http://vertx.io/docs/jsdoc/&https://github.com/vert-x3/vertx-web),但没有找到任何有用的东西。

If you need more info please let me know. Thanks in advance.

如果您需要更多信息,请告诉我。提前致谢。

EDIT 1:

I misunderstood what I was actually looking for in my original question and phrased it poorly. I know what the host and port of the EventBus are when I make the connection (i.e. var eb = new EventBus('localhost:8080/eventbus'); as @Paulo said).

我在我原来的问题中误解了我实际上在寻找什么,并且表达得很糟糕。我知道当我建立连接时EventBus的主机和端口是什么(即var eb = new EventBus('localhost:8080 / eventbus');正如@Paulo所说的那样)。

What I am actually looking for is the host and port for the Handler that my client registers on that EventBus. I can see this on the server side as mentioned above. The host comes back as an ip address, not what I passed in to create the EventBus connection on the client-side. And the port is different, because a new SockJS connection is made for that handler (?). Is it possible to get the host and port, especially port, of the SockJS connection for my client's Handler.

我实际需要的是我的客户在EventBus上注册的Handler的主机和端口。我可以在服务器端看到这个,如上所述。主机作为IP地址返回,而不是我传入的在客户端创建EventBus连接的内容。端口是不同的,因为为该处理程序(?)创建了一个新的SockJS连接。是否可以为我的客户端的Handler获取SockJS连接的主机和端口,尤其是端口。

1 个解决方案

#1


2  

The information you want to have is something you already know. When you create a eventbus connection from your client web application you do:

您想要的信息是您已经知道的。从客户端Web应用程序创建eventbus连接时,您执行以下操作:

var eb = new EventBus('http://localhost:8080/eventbus');

As you can see the parameter you must pass to the constructor contains what you want to know:

正如您所看到的,您必须传递给构造函数的参数包含您想要知道的内容:

localhost:8080

Most of the times you do not want to use an hardcoded hostname + port but the location where your html has been served so in JavaScript you can just do:

大多数情况下,您不希望使用硬编码的主机名+端口,而是使用html的位置,因此在JavaScript中您可以这样做:

window.location.host

And that will give you the location where your server is running.

这将为您提供服务器运行的位置。

#1


2  

The information you want to have is something you already know. When you create a eventbus connection from your client web application you do:

您想要的信息是您已经知道的。从客户端Web应用程序创建eventbus连接时,您执行以下操作:

var eb = new EventBus('http://localhost:8080/eventbus');

As you can see the parameter you must pass to the constructor contains what you want to know:

正如您所看到的,您必须传递给构造函数的参数包含您想要知道的内容:

localhost:8080

Most of the times you do not want to use an hardcoded hostname + port but the location where your html has been served so in JavaScript you can just do:

大多数情况下,您不希望使用硬编码的主机名+端口,而是使用html的位置,因此在JavaScript中您可以这样做:

window.location.host

And that will give you the location where your server is running.

这将为您提供服务器运行的位置。