• spring topic chat


    
    
    @MessageMapping("/chat")
    public void handleChat(Principal principal,String msg){
    if(principal.getName().equals("admin")){
    simpMessagingTemplate.convertAndSendToUser("wisely","/queue/notifications",principal.getName() + "-send:" + msg);
    }else{
    simpMessagingTemplate.convertAndSendToUser("admin","/queue/notifications",principal.getName() + "-send:" + msg);
    }
    }
    @Configuration
    public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
    super.addViewControllers(registry);
    registry.addViewController("/ws").setViewName("/ws");
    registry.addViewController("/login").setViewName("/login");
    registry.addViewController("/chat").setViewName("/chat");
    }
    }
    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    http
    .authorizeRequests()
    .antMatchers("/","/login").permitAll()//1根路径和/login路径不拦截
    .anyRequest().authenticated()
    .and()
    .formLogin()
    .loginPage("/login") //2登陆页面
    .defaultSuccessUrl("/chat") //3登陆成功转向该页面
    .permitAll()
    .and()
    .logout()
    .permitAll();

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
    .withUser("admin").password("admin").roles("USER")
    .and()
    .withUser("wisely").password("wisely").roles("USER");
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/resources/static/**");
    }
    }
    @Configuration
    @EnableWebSocketMessageBroker
    public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{
    @Override
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
    stompEndpointRegistry.addEndpoint("/endpointWisely").withSockJS();
    stompEndpointRegistry.addEndpoint("/endpointChat").withSockJS();


    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
    super.configureMessageBroker(registry);
    registry.enableSimpleBroker("/queue","/topic");
    }
    }
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <meta charset="UTF-8" />
    <head>
    <title>登陆页面</title>
    </head>
    <body>
    <div th:if="${param.error}">
    无效的账号和密码
    </div>
    <div th:if="${param.logout}">
    你已注销
    </div>
    <form th:action="@{/login}" method="post">
    <div><label> 账号 : <input type="text" name="username" /> </label></div>
    <div><label> 密码: <input type="password" name="password" /> </label></div>
    <div><input type="submit" value="登陆"/></div>
    </form>
    </body>
    </html>
    <!DOCTYPE html>

    <html xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8" />
    <head>
    <title>Home</title>
    <script th:src="@{sockjs.js}"></script>
    <script th:src="@{stomp.js}"></script>
    <script th:src="@{jquery.js}"></script>
    </head>
    <body>
    <p>
    聊天室
    </p>

    <form id="wiselyForm">
    <textarea rows="4" cols="60" name="text"></textarea>
    <input type="submit"/>
    </form>

    <script th:inline="javascript">
    $('#wiselyForm').submit(function(e){
    e.preventDefault();
    var text = $('#wiselyForm').find('textarea[name="text"]').val();
    sendSpittle(text);
    });

    var sock = new SockJS("/endpointChat"); //1
    var stomp = Stomp.over(sock);
    stomp.connect('guest', 'guest', function(frame) {
    stomp.subscribe("/user/queue/notifications", handleNotification);//2
    });

    function handleNotification(message) {
    $('#output').append("<b>Received: " + message.body + "</b><br/>")
    }

    function sendSpittle(text) {
    stomp.send("/chat", {}, text);//3
    }
    $('#stop').click(function() {sock.close()});
    </script>

    <div id="output"></div>
    </body>
    </html>
     
     
     
     









  • 相关阅读:
    .net core 在 Docker 上的部署
    js 运算的内置函数
    vux 项目的构建
    微信小程序开发资料
    HttpClient 调用WebAPI时,传参的三种方式
    jsplumb 中文教程
    MyEclipse最新版-版本更新说明及下载
    如何用VSCode调试Vue.js
    vs2017开发Node.js控制台程序
    Objc的底层并发API
  • 原文地址:https://www.cnblogs.com/yangfei-beijing/p/6279835.html
Copyright © 2020-2023  润新知