SimpMessagingTemplate dsnt在Tomcat的带注释的Spring版本中接收消息

时间:2018-10-18 08:55:20

标签: java websocket

我在StackOverflow-https://github.com/gerrytan/wsproblem上的某些问题中找到了此示例。而这个人在我的码头上也能很好地工作。

因此,我决定退出xml并将该示例重构为注释和Tomcat。那就是我现在所拥有的:

我的MVCConfig:

public class MVCConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    @Bean
    public InternalResourceViewResolver jspViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setOrder(1);
        viewResolver.setPrefix("/WEB-INF/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
}

我的根配置:

@Configuration
@ComponentScan(basePackages = { "com.javamaster.controller"}, excludeFilters={
        @ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class)
})
@Import({SecurityConfig.class, WebSocketConfig.class})
public class RootConfig {    
}

我的安全配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .passwordEncoder(NoOpPasswordEncoder.getInstance())
                .withUser("bob").password("1234").roles("USER");
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .antMatchers("/login/**").access("hasRole('IS_AUTHENTICATED_ANONYMOUSLY')")
                .antMatchers("/resources/**").access("hasRole('ROLE_USER')")
                .antMatchers("/**").access("hasRole('ROLE_USER')")
                .and().formLogin().defaultSuccessUrl("/", false);
    }

我的WebConfig:

public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(RootConfig.class, SecurityConfig.class);
        servletContext.addListener(new ContextLoaderListener(rootContext));
        AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext();
        servletAppContext.register(MVCConfig.class);
        DispatcherServlet dispatcherServlet = new DispatcherServlet(servletAppContext);
        dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet);
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] {AppSecurityConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[0];
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[0];
    }

我的WebSocket配置:

  @Configuration
    @EnableWebSocketMessageBroker
    public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {


        @Override
        public void configureMessageBroker(MessageBrokerRegistry config) {
            config.enableSimpleBroker("/topic", "/user");
            config.setApplicationDestinationPrefixes("/app");
        }

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

我的控制器:

@Controller
@RequestMapping("/")
public class HomeController {
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;
    @RequestMapping(method = RequestMethod.GET)
    public String home() {
        System.out.println("WE ARE HERE....");
        return "home";
    }
    @MessageMapping("/greeting")
    public void greeting(Principal principal) throws Exception {

        String reply = "hello " + principal.getName();
        System.out.println("sending " + reply);
        simpMessagingTemplate.convertAndSendToUser(principal.getName(), "/reply", reply);
    }
}

以及我在资源管理器控制台中拥有的内容:

打开Web套接字... stomp.js:130 Web套接字已打开... stomp.js:130 >>>连接 登录:访客 密码:客人 accept-version:1.1,1.0 心跳:10000,10000

所以,您怎么看-一切正常,但是@MessageMapping(“ / greeting”)不起作用,System.out.println(“ send” + Reply)在IDEA控制台中不打印任何内容,因此,将这种方法设置为Suumery不打电话。为什么?但是在GITHub的XML和Jetty版本中,我在控制台中收到:

<<<消息 内容类型:application / json; charset = UTF-8 订阅:sub-0 讯息编号:tmapc8ak-0 目的地:/用户/鲍勃/回复 内容长度:11 “你好鲍勃”

出了什么问题?

0 个答案:

没有答案
相关问题