SpringWebSockets不会从服务器发送消息

时间:2016-03-30 21:28:50

标签: java spring spring-integration spring-websocket

嗨我有弹簧websockets的问题,这就是场景:

一个独立的应用程序正在发送(远程)一些数据,如date Date,procedence String和weight BigDecimal,这个数据通过TCP发送到socket,

之后此数据保存到数据库中此时一切正常,但在下一步(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;
    }
}
在debbuger中我得到了这个信息:

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

在另一个类(Controller)中我使用相同的方法:

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

并“手动”发送正确显示的信息。我正在接受debbuger这条消息:

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

区别在于会话值,但我不知道这个会话在这个过程中是什么,我做错了什么澄清o欢迎帮助

1 个答案:

答案 0 :(得分:0)

首先,我没有看到configureMessageBroker实施,所以目前尚不清楚它是如何起作用的......

另一方面,如果您发现这种差异,请尝试调试SimpMessagingTemplate中的代码。

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

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