前端WebSocket进行消息实时推送和提示(附代码)

小咪咪 2023-06-19 11:16 102阅读 0赞

功能举例: 通过特定操作实时推送到页面反馈进行弹窗和播放音乐。

先贴源代码地址: 点我GO


  • 引入pom

创建一个基础的Spring Boot工程(没有特定版本),并在pom.xml中引入需要的依赖内容:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-freemarker</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-websocket</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-test</artifactId>
  16. <scope>test</scope>
  17. </dependency>

  • 配置配置文件(看需要配置) :

    spring.freemarker.template-loader-path=classpath:/templates
    spring.mvc.static-path-pattern=/static/**


  • 在需要接收消息的页面进行配置 :


  • 配置WebSocket Bean :

    @Component
    public class WebSocketConfig {

    1. @Bean
    2. public ServerEndpointExporter serverEndpointConfigurator(){
    3. return new ServerEndpointExporter();
    4. }

    }


  • 实现webSocket

    @Component
    @ServerEndpoint(“/webSocket”)
    public class WebSocket {

    1. private Session session;
    2. private static CopyOnWriteArraySet<WebSocket> webSocketSet = new CopyOnWriteArraySet<>();
    3. @OnOpen
    4. public void onOpen(Session session){
    5. this.session = session;
    6. webSocketSet.add(this);
    7. System.out.println("新的连接 总数:"+webSocketSet.size());
    8. }
  1. @OnClose
  2. public void onColse(){
  3. webSocketSet.remove(this);
  4. System.out.println("断开连接 总数:"+webSocketSet.size());
  5. }
  6. @OnMessage
  7. public void onMessage(String message){
  8. System.out.println("收到客户端发来的消息:"+message);
  9. }
  10. public void sendMessage(String message){
  11. for (WebSocket webSocket : webSocketSet){
  12. System.out.println("广播消息:"+message);
  13. try {
  14. webSocket.session.getBasicRemote().sendText(message);
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. }
  20. }

  • 配置触发控制器

    @RequestMapping(“/shop”)
    public class ListController {

    1. @Autowired
    2. private WebSocket webSocket;
    3. /**
    4. * 接收推送来的消息的页面
    5. * @return
    6. */
    7. @RequestMapping(value ="/list")
    8. public String list(){
    9. return "list";
    10. }
    11. /**
    12. * 触发推送
    13. * 举例:list方法可以是订单列表,这个方法可以是下单的接口,下单后,list的订单列表提示收到新订单。
    14. * @return
    15. */
    16. @RequestMapping(value = "/test")
    17. @ResponseBody
    18. public String test(){
    19. webSocket.sendMessage("hello,来了新数据呢~");
    20. return "send over,Please return to the list request page。";
    21. }

    }


  • 配置websocket响应提示:

    websocket.onmessage = function (event) {

    1. console.log('收到消息:'+event.data)
    2. alert(event.data);
    3. var myAuto = document.getElementById('notice');
    4. console.log('开始播放音乐!15秒后停止...');
    5. myAuto.play();
    6. setTimeout(function () {
    7. myAuto.pause();
    8. console.log('音乐停止...')
    9. },15000)
    10. }

  • 测试展示:

请求list地址 http://localhost:8080/shop/list

format_png

后台输出:

format_png 1


触发请求条件: http://localhost:8080/shop/test

format_png 2

list页面接收到消息:

format_png 3


查看:

format_png 4


format_png 5

如有不当之处,请予指正! 如果你有什么疑问也欢迎随时联系我共同探讨。

Email:wdnnccey#gmail.com

你配不上自己的野心 也辜负了所受的苦难

发表评论

表情:
评论列表 (有 0 条评论,102人围观)

还没有评论,来说两句吧...

相关阅读