Springboot2-Broadcast Type WebSocket

Source: Internet
Author: User
Tags stomp

1.websocket,stomp,sockjs meaning

Websocket:websocket is a protocol that HTML5 begins to provide full-duplex communication on a single TCP connection.

SOCKJS:SOCKJS is a simulation of WebSocket technology. To deal with the problem that many browsers do not support the WebSocket protocol, an alternative sockjs is designed. After opening and making the SOCKJS, it will prefer the WebSocket protocol as the transport protocol, if the browser does not support the WebSocket protocol, then in other scenarios, select a better protocol to communicate.

STOMP: The formatting of messages used to define WebSocket.

2.springboot Code

Defines the content format of the WebSocket transport message.

The browser sends a message to the server:

Package Com.dyq.demo.dto;public class Socketrequestmessage {    private String requestmessage;    Public String Getrequestmessage () {        return requestmessage;    }}

The server sends a message to the browser:

Package Com.dyq.demo.dto;public class Socketresponsemessage {    private String responsemessage;    Public Socketresponsemessage (String responsemessage) {        this.responsemessage = responsemessage;    }    Public String Getresponsemessage () {        return responsemessage;    }}

WebSocket configuration file:

Package Com.dyq.demo.config;import Org.springframework.context.annotation.configuration;import Org.springframework.messaging.simp.config.messagebrokerregistry;import org.springframework.web.socket.config.annotation.*, @Configuration @enablewebsocketmessagebroker// Enable the STOMP protocol to transfer messages based on the agent (message Broker) public class Websocketconfig implements Websocketmessagebrokerconfigurer {    @ Override public    void Registerstompendpoints (Stompendpointregistry registry) {        //register a STOMP node with SOCKJS protocol        registry.addendpoint ("/customendpoint"). WITHSOCKJS ();    }    @Override public    void Configuremessagebroker (messagebrokerregistry config) {        // Config.setapplicationdestinationprefixes ("/app");        Subscribe and broadcast using the built-in message agent, and the destination header of the routed message begins with "/topic" or "/queue".        config.enablesimplebroker ("/topic", "/queue");}    }

Controller code:

 Package Com.dyq.demo.controller;import Com.dyq.demo.dto.socketrequestmessage;import Com.dyq.demo.dto.socketresponsemessage;import org.springframework.messaging.handler.annotation.MessageMapping; Import Org.springframework.messaging.handler.annotation.sendto;import Org.springframework.stereotype.Controller;    @Controllerpublic class Websocketcontroller {@MessageMapping ("/socket")//browser sends a request to the server by messagemapping mapping/socket address @SendTo ("/topic/getresponse")//when the server has a message, the browser that subscribes to the path in @sendto sends a message to public socketresponsemessage say (        Socketrequestmessage message) throws Interruptedexception {Thread.Sleep (3000); return new Socketresponsemessage ("Received Requestmessage:" +message.getrequestmessage () + "!");}} 

Front-End page code websocket.html:
Front-end requires three JS files, you can go to http://www.bootcdn.cn/search Download
Current latest version:
sockjs.min.js:https:// Cdn.bootcss.com/sockjs-client/1.1.4/sockjs.min.js
STOMP.MIN.JS:HTTPS://CDN.BOOTCSS.COM/STOMP.JS/2.3.3/ Stomp.min.js
Jquery.min.js:https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js

<! DOCTYPE html>

Required in the MVC configuration file:

Package Com.dyq.demo.config;import Org.apache.catalina.context;import Org.apache.catalina.connector.Connector; Import Org.apache.tomcat.util.descriptor.web.securitycollection;import Org.apache.tomcat.util.descriptor.web.securityconstraint;import Org.springframework.boot.web.embedded.tomcat.tomcatservletwebserverfactory;import Org.springframework.boot.web.servlet.server.servletwebserverfactory;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import org.springframework.web.servlet.config.annotation.*, @Configurationpublic class Mvcconfig implements webmvcconfigurer {public    void Addviewcontrollers (Viewcontrollerregistry registry) {        Registry.addviewcontroller ("/"). Setviewname ("/index");        Registry.addviewcontroller ("/index"). Setviewname ("/index");        Registry.addviewcontroller ("/websocket"). Setviewname ("/websocket");}    }

Run, open multiple browser windows, connect on, if a browser sends messages to the server, other windows can receive information.
Then run the effect:

Springboot2-Broadcast Type WebSocket

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.