SpringBootはWebsocketを統合し、ブロードキャストを実現し、特定のユーザーにメッセージを送信する


文書ディレクトリ
  • まとめ:
  • ここではプログラミングを採用しています.
  • 参考資料
  • の上にフロントエンド総404の問題が発生し、オリジナルのwebsocketを転用する可能性がある.
  • 注意点
  • まとめ:
  • ブロードキャスト:すべての人に通知し、簡単です.アドレスを1つ購読すればいいだけです.
  • 特定ユーザー:2つの方法.
  • は、個人が購読するアドレスが異なります.
  • プログラミング式でwebsocketを取得し、操作すればいいです.


  • ここではプログラミングで使います.
  • 構成bean:
  • 
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.messaging.simp.config.MessageBrokerRegistry;
    import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
    import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
    import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
    
    /**
     *     : wss://www.example.com/websocket/SendTo/topic/greetings
     *     : wss://www.example.com/websocket/MessageMapping/topic/MessageMapping
     * socket      
     */
    @Configuration
    @EnableWebSocketMessageBroker
    public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
    
        @Override
        public void configureMessageBroker(MessageBrokerRegistry config) {
            //
            config.enableSimpleBroker("/MessageMapping");// /users     
            config.setApplicationDestinationPrefixes("/SendTo");
            //         user              ,    
            config.setUserDestinationPrefix("/user-wlh/");
        }
    
        @Override
        public void registerStompEndpoints(StompEndpointRegistry registry) {
            //
            registry.addEndpoint("/websocket").withSockJS();
        }
    
    }
    
  • 具体コード:
  • 
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.messaging.MessageHeaders;
    import org.springframework.messaging.handler.annotation.MessageMapping;
    import org.springframework.messaging.handler.annotation.SendTo;
    import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
    import org.springframework.messaging.simp.SimpMessageType;
    import org.springframework.messaging.simp.SimpMessagingTemplate;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import java.io.IOException;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    
    @Controller
    @RequestMapping("/checkcenter")
    public class CheckCenterController {
    	/**
    	 * session   
    
    	@Autowired
    	SocketSessionRegistry webAgentSessionRegistry;	 */
    	/**
    	 *       
    	 */
    	@Autowired
    	private SimpMessagingTemplate template;
    
    	@RequestMapping(value = "/index")
    	public String index() {
    		template.convertAndSendToUser("userId", "/topic/greetings", "    ");
    		return "/index";
    	}
    
    	/**
    	 *     
    	 *                 
    	 *
    	 * @param request
    	 * @return
    	 */
    	@MessageMapping("/change-notice1")
    	public void greeting1(String value) {
    		this.template.convertAndSend("/topic/notice", value);
    	}
    	@MessageMapping("/change-notice")
    	@SendTo("/topic/notice")
    	public String greeting(String value) {
    		return value;
    	}
    
    
    	/**
    	 *               ws    http      
    	 *
    	 * @param message
    	 * @return
    	 * @throws Exception
    	 */
    	@MessageMapping("/msg/hellosingle")
    	public void greeting2(String userId) throws Exception {
    		//      
    		template.convertAndSendToUser(userId, "/topic/greetings", "    ");
    	}
    
    } 
    

    参考資料
  • SpringBoot 2.0はWebSocketを統合し、バックグラウンドからフロントエンドに情報をプッシュする
  • を実現する.
    彼の原理を見ればdemoは通じない
  • spring boot 2.xに基づくwebsocket例
  • 基本demoは、グローバルプッシュ、グローバルプッシュの簡易書き方のみです.
  • Spring WebSocketプローブ1(Spring WebSocket入門チュートリアル)
  • これはjavaバックエンドです.
  • Spring WebSocketプローブ2(Spring WebSocket入門チュートリアル)
  • これはjsフロントエンドです.
  • SpringBootはWebsocketを統合し、ブロードキャストと特定のユーザーへのメッセージの送信を実現し、ログインを必要とせずにメッセージを送信する.

  • demoと
  • SpringBoot WebSocket指定したユーザーにメッセージを送信-注釈版
  • @SendToUserで特定のユーザーに送信
  • Spring websocket+Stomp+SockJSリアルタイム通信詳細
  • bootファミリーバケツ使用案.
    フロントエンド総404の問題が発生し、オリジナルのwebsocketを転用する可能性があります.
    見てごらん
  • SpringBoot 2統合WebSocket
  • ブロードキャストの実装
  • websocket@ServerEndpoint(value="/websocket/{ip}))詳細
  • 特定のユーザーにメッセージを送信
  • WebSocket javax.websocket.RemoteEndpoint.Basic.sendObject(Object arg 0)を使用してオブジェクト
  • をページメソッドに送信
    使うのをやめて、これはもっと面倒です.
    注意点
  • spring-boot-junit @SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  • を追加する必要があります.
  • appクラスは、注釈websocket注釈を追加して起動できません.そうしないと、エラーが発生します.