美文网首页
java,springboot实现websocket消息推送

java,springboot实现websocket消息推送

作者: 小吖么小一郎 | 来源:发表于2019-07-05 11:20 被阅读0次

    加入依赖

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-websocket</artifactId>
            </dependency>
    

    新建一个WebSocketConfig.java文件

    package com.example.demo.config;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.socket.server.standard.ServerEndpointExporter;
    @Configuration
    public class WebSocketConfig {
        @Bean
        public ServerEndpointExporter serverEndpointExporter(){
            return new ServerEndpointExporter();
        }
    }
    

    新建一个 WebSocketController.java文件

    package com.example.demo.controller;
    import org.springframework.stereotype.Component;
    import javax.websocket.*;
    import javax.websocket.server.ServerEndpoint;
    import java.io.IOException;
    import java.util.concurrent.CopyOnWriteArraySet;
    @ServerEndpoint("/websocket")
    @Component
    public class WebsocketController {
        private static int onlineCount = 0;
        private static CopyOnWriteArraySet<WebsocketController> webSocketSet = new CopyOnWriteArraySet<WebsocketController>();
        private Session session;
        /**
         * 成功建立连接调用
         */
        @OnOpen
        public void onOpen(Session session){
            this.session = session;
            webSocketSet.add(this);     
            addOnlineCount();           
            System.out.println("有用户加入直播间,当前在线人数为" + getOnlineCount());
        }
        /**
         * 连接关闭调用的方法
         */
        @OnClose
        public void onClose(){
            webSocketSet.remove(this);  //删除连接
            subOnlineCount();           //在线人数减1
            System.out.println("有人退出直播,当前在线人数为" + getOnlineCount());
        }
        /**
         * 收到客户端消息后调用的方法
         */
        @OnMessage
        public void onMessage(String message, Session session) {
            System.out.println("来自客户端的消息:" + message);
            //群发消息
            for(WebsocketTest item: webSocketSet){
                try {
                    item.sendMessage(message);
                } catch (IOException e) {
                    e.printStackTrace();
                    continue;
                }
            }
        }
        /**
         * 发生错误时调用
         */
        @OnError
        public void onError(Session session, Throwable error){
            System.out.println("发生错误");
            error.printStackTrace();
        }
        public static synchronized int getOnlineCount() {
            return onlineCount;
        }
        public static synchronized void addOnlineCount() {
            WebsocketTest.onlineCount++;
        }
        public static synchronized void subOnlineCount() {
            WebsocketTest.onlineCount--;
        }
    }
    

    相关文章

      网友评论

          本文标题:java,springboot实现websocket消息推送

          本文链接:https://www.haomeiwen.com/subject/rxyshctx.html