4

I am trying to get response from server to browser using spring websockets. Periodically server sends messages to the client.

My problem is, I have two pages. One is directly under the webapps and it's a jsp page, and other is under webapps->pages->html, it is a html page. The page which is direclty under the webapps folder can receive the server response.Here is how I connect. Both pages have same code.

    var url='/WBS/secure/simplemessages';
            var socket = new SockJS(url);
            stompClient = Stomp.over(socket);
            stompClient.connect('user', 'guest', function(frame) {
                stompClient.subscribe("/topic/simplemessagesresponse", function(servermessage) {//Callback when server responds
                alert('sm: '+servermessage);
                    showServerBroadcast((servermessage.body).messageContent, false);
                });
            });  

Response comes back as `Web Socket Opened... stomp.js:134

CONNECT login:user passcode:guest accept-version:1.1,1.0 heart-beat:10000,10000`

According to the prefix, my web.xml has also been changed. I use Spring 4.1.0 and here is web.xml

  <servlet>
    <servlet-name>sample</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            com.action.WebSocketConfig
        </param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>sample</servlet-name>
    <url-pattern>/secure/*</url-pattern>
</servlet-mapping>

Here is my websocketconfigure class..

@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
@Component
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

      @Autowired  
      private SimpMessagingTemplate template;

      private TaskScheduler scheduler = new ConcurrentTaskScheduler();
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/simplemessages").withSockJS();
    }

    public void configureMessageBroker(MessageBrokerRegistry config) {

        config.enableSimpleBroker("/topic/", "/queue/");
        config.setApplicationDestinationPrefixes("/app");
    }

    @PostConstruct
    private void broadcastTimePeriodically() {

      scheduler.scheduleAtFixedRate(new Runnable() {
         public void run() {
             template.convertAndSend("/topic/simplemessagesresponse", "server sending back -response"+new Date());

        }
      }, 4000);

    }

    public void configureClientInboundChannel(ChannelRegistration registration) {
    }

   public void configureClientOutboundChannel(ChannelRegistration registration) {
      registration.taskExecutor().corePoolSize(4).maxPoolSize(10);
  }
    public boolean configureMessageConverters(List<MessageConverter> arg0) {
        return true;
    }
    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration arg0) {
    }
}

in the method, broadcastTimePeriodically() periodically I send a message. I checked the schedular working and found it works well.

Any one let me know why one page cannot have the response from server which is sent periodically.

Débora
  • 5,816
  • 28
  • 99
  • 171

0 Answers0