springboot集成websocket

1.pom引入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.78</version>
        </dependency>
         <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.4.0</version>
        </dependency>

2.注入ServerEndpointExporter

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
public class WebSocketConfig {
    /**
     * 	注入ServerEndpointExporter,
     * 	这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

}

3.JWT工具类,原来签名,解密用户数据

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;


public class JwtUtils {

    // 用于JWT进行签名加密的秘钥
    private static String SECRET = "ab35trc-*%#@*!&";

    public static final String TOKEN_KEY = "token";
    //60 * 60 * 24 * 1
    public static final int EXP_SECOND = 60 * 60 * 24 * 1;

    /**
     * @Param: 传入需要设置的payload信息,用户信息生成签名
     * @return: 返回token
     */
    public static String generateToken(Map<String, String> map) {
        JWTCreator.Builder builder = JWT.create();

        // 将map内的信息传入JWT的payload中
        map.forEach((k, v) -> {
            builder.withClaim(k, v);
        });

        // 设置JWT令牌的过期时间为60
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.SECOND, EXP_SECOND);
        builder.withExpiresAt(instance.getTime());

        // 设置签名并返回token
        return builder.sign(Algorithm.HMAC256(SECRET)).toString();
    }

    /**
     * @Param: 传入token,校验并且解密数据
     * @return:
     * 校验失败会抛出JWTDecodeException解析异常,TokenExpiredException过期异常,算法异常,签名异常等等
     */
    public static DecodedJWT getTokenInfo(String token) {
      return JWT.require(Algorithm.HMAC256(SECRET)).build().verify(token);
    }




}

4.创建WebSocket服务端

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

import com.alibaba.fastjson.JSON;
import com.kingmed.wxpusher.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


@Component
@Slf4j
@ServerEndpoint("/webSocket/{userId}")  // 接口路径 ws://localhost:8087/webSocket/userId;
public class WebSocket {

        //与某个客户端的连接会话,需要通过它来给客户端发送数据
        private Session session;
        /**
         * 用户ID
         */
        private String userId;
        //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
        //虽然@Component默认是单例模式的,但springboot还是会为每个websocket连接初始化一个bean,所以可以用一个静态set保存起来。
        //  注:底下WebSocket是当前类名
        private static CopyOnWriteArraySet<WebSocket> webSockets =new CopyOnWriteArraySet<>();
        // 用来存在线连接用户信息
        private static ConcurrentHashMap<String,Session> sessionPool = new ConcurrentHashMap<String,Session>();

        /**
         * 链接成功调用的方法,userId是一个JWT签名,里面包含了用户的一些数据
         */
        @OnOpen
        public void onOpen(Session session, @PathParam(value="userId")String userId) {
            //从前端传过来token里面获取用户账号
            String phone = JwtUtils.getTokenInfo(userId).getClaim("phone").asString();
            try {
                this.session = session;
                this.userId = phone;
                webSockets.add(this);
                sessionPool.put(phone, session);
                log.info("【websocket消息】有新的连接,总数为:"+sessionPool.size());
                sessionPool.forEach((k,index)->log.info("【websocket消息】当前在线用户:"+k));
            } catch (Exception e) {
            }
        }

        /**
         * 链接关闭调用的方法
         */
        @OnClose
        public void onClose() {
            try {
                webSockets.remove(this);
                sessionPool.remove(this.userId);
                log.info("【websocket消息】连接断开,总数为:"+webSockets.size());
            } catch (Exception e) {
            }
        }
        /**
         * 收到客户端消息后调用的方法
         *
         * @param message
         * @param session
         */
        @OnMessage
        public void onMessage(String message) {
            log.info("【websocket消息】收到客户端消息:"+message);
        }

        /** 发送错误时的处理
         * @param session
         * @param error
         */
        @OnError
        public void onError(Session session, Throwable error) {

            log.error("用户错误,原因:"+error.getMessage());
            error.printStackTrace();
        }


        // 此为广播消息
        public void sendAllMessage(String message) {
            log.info("【websocket消息】广播消息:"+message);
            for(WebSocket webSocket : webSockets) {
                try {
                    if(webSocket.session.isOpen()) {
                        webSocket.session.getAsyncRemote().sendText(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        // 此为单点消息
        public void sendOneMessage(String userId, String message) {
            Session session = sessionPool.get(userId);
            if (session != null&&session.isOpen()) {
                try {
                    log.info("【websocket消息】单点消息:"+message);
                     session.getAsyncRemote().sendText(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
       // 此为单点消息
        public void sendOneMessage(SocketEntity socket) {
            Session session = sessionPool.get(socket.getUserId());
            if (session != null&&session.isOpen()) {
                try {
                    log.info("【websocket消息】单点消息:"+socket.getMsg());
                    session.getAsyncRemote().sendText(JSON.toJSONString(socket));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        // 此为单点消息(多人)
        public void sendMoreMessage(String[] userIds, String message) {
            for(String userId:userIds) {
                Session session = sessionPool.get(userId);
                if (session != null&&session.isOpen()) {
                    try {
                        log.info("【websocket消息】 单点消息:"+message);
                        session.getAsyncRemote().sendText(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

        }
}

5.SocketEntity封装的一个消息对象

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SocketEntity {
    private String userId;
    private String msg;
    private String status;
    private String type;

    public SocketEntity (String userId,String msg){
        this.userId=userId;
        this.msg=msg;
    }
    public SocketEntity (String userId,String msg,String status){
        this.userId=userId;
        this.msg=msg;
        this.status=status;
    }
}

6.注入即可使用

    @Autowired
    private WebSocket webSocket;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值