gpt4 book ai didi

sockets - Spring Integration TCP - 在发送数据之前用消息启动握手

转载 作者:可可西里 更新时间:2023-11-01 02:50:23 30 4
gpt4 key购买 nike

我正在使用@MessagingGateway 将数据发送到服务器。我为我的出站网关配置了一个 AbstractClientConnectionFactory 和一个@ServiceActivator。

为了向我的服务器发送数据,我需要在连接启动时发送握手消息。如果来自服务器的响应是我期望的握手响应,那么我将发送有意义的数据。我最初的解决方案是

if (gateway.handshake(HANDSHAKE).equals(HANDSHAKE_RESPONSE))
网关.sendData(数据);

这在我扩展时不太好,因为我通过 tcp 的调用增加了一倍,因为我只需要在连接启动时发送发送握手,而不是每次都发送。另外,我计划保持连接。

那么在连接启动时,我怎样才能实现这个自定义握手?

@Bean
public AbstractClientConnectionFactory clientCF() {
TcpNetClientConnectionFactory tcpNetClientConnectionFactory = new TcpNetClientConnectionFactory(host, port);

tcpNetClientConnectionFactory.setSerializer(new serializerDeserializer());
tcpNetClientConnectionFactory.setDeserializer(new serializerDeserializer());
tcpNetClientConnectionFactory.setSoKeepAlive(true);
tcpNetClientConnectionFactory.setSoTimeout(soTimeout);
return tcpNetClientConnectionFactory;

}

@Bean
@ServiceActivator(inputChannel = "toTcp")
public MessageHandler tcpOutGate(AbstractClientConnectionFactory connectionFactory) {
TcpOutboundGateway gate = new TcpOutboundGateway();
gate.setConnectionFactory(connectionFactory);
return gate;
}


@Bean
public MessageChannel fromTcp() {
return new DirectChannel();
}

最佳答案

参见 Connection Interceptors .

Connection factories can be configured with a reference to a TcpConnectionInterceptorFactoryChain. Interceptors can be used to add behavior to connections, such as negotiation, security, and other setup. No interceptors are currently provided by the framework but, for an example, see the InterceptedSharedConnectionTests in the source repository.

The HelloWorldInterceptor used in the test case works as follows:

...

虽然框架中没有标准拦截器,但有 test cases that show how to use them for an initial handshake, for example herehere .

测试比您需要的更复杂,因为它们测试多个嵌套拦截器。

hello world test interceptors首次打开套接字时发送 Hello 并期待 World! 。他们为客户端和服务器端实现握手。

关于sockets - Spring Integration TCP - 在发送数据之前用消息启动握手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48632414/

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