gpt4 book ai didi

c# - 如何使用 SslStream 连接进行 ActiveMQ 通信

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:20 25 4
gpt4 key购买 nike

我想与服务器建立连接并与该服务器上运行的 ActiveMQ 通信我从 MSDN 获取代码:

TcpClient client = new TcpClient(machineName, port); // ActiveMQ is running on that port on the server
Console.WriteLine("Client connected.");
SslStream sslStream = new SslStream(
client.GetStream(),
false,
new RemoteCertificateValidationCallback(ValidateServerCertificate),
null
);
try
{
sslStream.AuthenticateAsClient(machineName, certificates, SslProtocols.Tls, true);
}
catch (AuthenticationException e)
{
Console.WriteLine("Exception: {0}", e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
}
Console.WriteLine("Authentication failed - closing the connection.");
client.Close();
return;
}

并且我能够建立连接。我向服务器发送请求:

byte[] messsage = Encoding.UTF8.GetBytes("Hello from the client.<EOF>");
// Send hello message to the server.
sslStream.Write(messsage);
sslStream.Flush();
// Read message from the server.
string serverMessage = ReadMessage(sslStream);
Console.WriteLine("Server says: {0}", serverMessage);

在响应中,服务器说:

?ActiveMQ y TcpNoDelayEnabled SizePrefixDisabled CacheSize ProviderName ActiveMQ StackTraceEnabled PlatformDetails TJVM: 1.8.0_202, 25.202-b08, Oracle Corporation, OS: Windows Server

因此,这意味着与服务器 AMQ 的通信已建立。我的问题是,我现在如何使用 AMQ channel 发送和接收消息?通常,我必须指定要使用的消息队列。我必须如何以及在何处执行此操作?它应该在我发送到服务器的消息中吗?像这样的东西:

string message_queue = "test.message.tosend";
byte[] request = Encoding.UTF8.GetBytes(String.Format(<formatted queue name with message content>, machineName));

代码示例将不胜感激。

最佳答案

ActiveMQ 是一个消息代理,它支持许多不同的消息传递协议(protocol)(例如 OpenWire、AMQP 1.0、STOMP 和 MQTT)。这些协议(protocol)中的每一个都相似,因为它们支持客户端和代理之间的消息交换,但它们在支持的特定功能、协议(protocol)握手、有线格式等方面有所不同。

无论出于何种原因,您都没有使用这些协议(protocol)的任何客户端实现。您正在使用低级别 TcpClient这意味着您需要找到您希望使用的协议(protocol)的规范(例如 AMPQ 1.0STOMP 1.2 )并为其实现一个客户端。根据您选择的协议(protocol),这可能需要大量工作。

但是,您可以使用已经存在的客户端,而不是重新实现客户端,例如:

关于c# - 如何使用 SslStream 连接进行 ActiveMQ 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58093436/

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