SpringBoot整合websocket

什么是Websocket

websocket使得客户端和服务器之间的数据交换变得更加简单,他是全双工通信,我们可以利用浏览器给服务器发送数据,也允许服务端主动向客户端推送数据。在websocket api中浏览器和服务器只需完成一次握手,两者之间直接可以创建持久性的连接,并进行双向传输数据。

1.添加相关依赖

	 
        org.springframework.boot
        spring-boot-starter-websocket
    
    
    
        org.webjars
        sockjs-client
        1.1.2
    
    
        org.webjars
        jquery
        3.4.1
    
    
        org.webjars
        stomp-websocket
        2.3.3
    
    
    
        org.webjars
        webjars-locator-core
    

2.配置websocket

package com.gy.demowebsocket.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker//表示开启使用STOMP协议来传输基于代理的消息,Broker就是代理的意思。
public class WebsocketConfig implements WebSocketMessageBrokerConfigurer {
    /**
     * 注册stomp的端点
    * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        //允许使用socketJs方式访问,访问点为chat,允许跨域
        //在网页上我们可以通过这个链接
        //http://localhost:8080/chat
        //来和服务器的websocket连接
        registry.addEndpoint("/chat").setAllowedOrigins("*").withSockJS();//与socket建立连接
    }

    /**
     * 配置信息代理
     * @param registry
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        //订阅Broker的名称(群聊)
        registry.enableSimpleBroker("/topic");
        //全局使用的消息前缀(客户端订阅路径上会体现出来)
        registry.setApplicationDestinationPrefixes("/app");
    }
}

3.消息实体类

package com.gy.demowebsocket.bean;

public class Message {
    private String name;
    private String content;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

4.控制层

package com.gy.demowebsocket.controller;

import com.gy.demowebsocket.bean.Message;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class GreetingController {

    @Resource
    SimpMessagingTemplate simpMessagingTemplate;

   /* @MessageMapping("/hello")
    @SendTo("/topic/greeting")
    public Message greeting(Message message) {
        return message;
    }*/
   //与上边是等价的
   @MessageMapping("/hello")
   public void greeting(Message message) {
       simpMessagingTemplate.convertAndSend("/topic/greeting",message);
   }




}

5.前端页面




    
    Title
    
    
    



请输入用户名:

6.结果展示

SpringBoot整合websocket_第1张图片

你可能感兴趣的:(SpringBoot)