zoukankan      html  css  js  c++  java
  • 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>
     
     
     
     









  • 相关阅读:
    使用respondsToSelector:来发现对象是否响应消息
    使用iskindofclass来发现对象是否是某类或其子类的实例
    集合set的使用
    字典的使用
    数组的使用
    对NSNumber的理解
    数组、字典和集合的定义
    强引用strong和弱引用weak的定义
    类工厂方法的定义
    POJ 2262 / UVa 543
  • 原文地址:https://www.cnblogs.com/yangfei-beijing/p/6279835.html
Copyright © 2011-2022 走看看