【问题标题】:SpringWebSockets does not send message from serverSpringWebSockets 不从服务器发送消息
【发布时间】:2016-07-19 02:53:41
【问题描述】:

您好,我对 spring websockets 有疑问,这是场景:

一个独立的应用程序正在发送(远程)一些数据,例如日期、过程字符串和权重 BigDecimal,这些数据通过 TCP 发送到套接字,

在此数据保存到数据库之后一切都很好,但在下一步(websocket)中,我无法在网页中显示此信息,必须在屏幕上显示(实时)重量数据

这是我的 websocket 配置:

import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(final StompEndpointRegistry registry) {
        registry.addEndpoint("/indicator").withSockJS();
    }

    @Override
    public void configureClientInboundChannel(final ChannelRegistration registration) {
    }

    @Override
    public void configureClientOutboundChannel(final ChannelRegistration registration) {
    }

    @Override
    public void configureMessageBroker(final MessageBrokerRegistry registry) {

    }

    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration wstr) {

    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> list) {

    }

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> list) {

    }

    @Override
    public boolean configureMessageConverters(List<MessageConverter> list) {
        return Boolean.TRUE;
    }

}

这是我的另一个类,它接收来自套接字的数据并处理信息并发送到 websocket:

import com.mcss.mcontrols.helper.ByteHelper;
import com.spc.basweb.Constants;
import com.spc.basweb.transmissor.dto.Transmission;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.messaging.core.MessageSendingOperations;
import org.springframework.messaging.simp.broker.BrokerAvailabilityEvent;
import com.spc.basweb.service.BroadcastingService;
import com.spc.basweb.service.DataProcessorService;
import java.io.IOException;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;

@MessageEndpoint
public class BroadcastingServiceImpl implements BroadcastingService, ApplicationListener<BrokerAvailabilityEvent> {

    private static final Logger LOGGER = Logger.getLogger(BroadcastingServiceImpl.class);
    private final MessageSendingOperations<String> messagingTemplate;
    private String processedData;

    @Autowired
    DataProcessorService dataProcessorService;

    @Autowired
    public BroadcastingServiceImpl(final MessageSendingOperations<String> messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }

    @Override
    public String getProcessedData() {
        return processedData;
    }

    @Override
    @ServiceActivator(inputChannel = "broadcaster")
    public String broadcast(byte[] bytes) {
        try {
            Transmission t = (Transmission) ByteHelper.toObject(bytes);
            LOGGER.debug(t.getProcedence() + " " + t.getDate() + " " + t.getWeight());
            String rm = this.dataProcessorService.processData(t);
            this.messagingTemplate.convertAndSend(Constants.END_POINT_READ, this.dataProcessorService.getWeighing().getWeight().toString());
            return rm;
        } catch (IOException | ClassNotFoundException ex) {
            LOGGER.error("Error de transmision de objetos", ex);
        }
        return DataProcessorService.NOT_OK_RESPONSE;
    }

    @Override
    public void onApplicationEvent(BrokerAvailabilityEvent e) {
        LOGGER.debug("Application event");
    }

    @Transformer(outputChannel = "broadcaster")
    public String convert(String response) {
        return response;
    }
}

在调试器中我得到了这个信息:

30-03-2016 15:07:20 DEBUG SimpleBrokerMessageHandler:277 - Processing MESSAGE destination=/read session=null payload=3003

在另一个类(控制器)中,我使用相同的方法:

this.messagingTemplate.convertAndSend(Constants.END_POINT_READ, "3500");

并“手动”发送正确显示的信息。我正在调试器中收到这条消息:

30-03-2016 15:05:18 DEBUG SimpleBrokerMessageHandler:277 - Processing MESSAGE destination=/read session=dfR45V77 payload=3500

不同之处在于会话值,但我不知道此会话在此过程中有什么为 null,我做错了什么欢迎澄清或帮助

【问题讨论】:

    标签: java spring spring-integration spring-websocket


    【解决方案1】:

    首先我没有看到configureMessageBroker 的实现,所以根本不清楚它是如何工作的......

    另一方面,如果您看到这样的差异,请尝试调试SimpMessagingTemplate 中的代码。

    我只在SimpleBrokerMessageHandler 中看到headerAccessor.setSessionId(sessionId);

     logger.debug("Broadcasting to " + subscriptions.size() + " sessions.");
    

    【讨论】:

    • 您好,感谢您的快速响应,我不知道如何配置 MessageBroker,但您在此过程中是对的,我在注册表中没有订阅者,但我也不知道为什么,直接来自发送此调试 30-03-2016 16:07:42 调试 SimpleBrokerMessageHandler:301 - 广播到 1 个会话后,我拥有控制器。
    • 在网络记录器中我有这个:
    • 您可以在 Spring Framework 参考手册中找到如何配置 Simple Broker:docs.spring.io/spring/docs/current/spring-framework-reference/…。所以,你真的没有那个目的地的订阅者。只有当有一些订阅者时,你才能真正发送消息,否则会话标头是null
    • 我将这些行添加到 configureMessageBroker:@Override public void configureMessageBroker(final MessageBrokerRegistry registry) { registry.setApplicationDestinationPrefixes(Constants.DESTINATION_PREFIX); registry.enableSimpleBroker(常量。END_POINT_READ); } 但我没有结果是完全一样的,我仍然有 0 个订阅者为此上下文,另一个建议??
    • 您的应用程序的客户是谁?为什么不将其订阅到所需的目的地?
    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 2022-06-29
    • 1970-01-01
    • 2020-10-30
    相关资源
    最近更新 更多