Spring WebSocket不会将数据发送回客户端订户

时间:2014-09-10 12:05:59

标签: java javascript spring spring-mvc websocket

我正在尝试使用spring websockets从服务器到浏览器的响应。服务器定期向客户端发送消息。

我的问题是,我有两页。一个是直接在webapps下面,它是一个jsp页面,另一个在 webapps-> pages-> html 下,它是一个html页面。 webapps文件夹下的direclty页面可以接收服务器响应。这就是我连接的方式。两个页面都有相同的代码。

    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);
                });
            });  

响应回来了`Web Socket Opened ... stomp.js:134

  
    
      

CONNECT       登录:用户       密码:游客       接受版本:1.1,1.0       心脏搏动:10000,10000`

    
  

根据前缀,我的web.xml也被更改了。我使用的是Spring 4.1.0,这里是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>

这是我的websocketconfigure类..

@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) {
    }
}

在方法中,broadcastTimePeriodically()会定期发送消息。我检查了调度程序的工作情况,发现它运行良好。

任何人都让我知道为什么一个页面无法定期发送服务器的响应。

0 个答案:

没有答案