解决socketjs连接不上ios问题

cocos 游戏打包Android 端和网页端socketjs连接正常   ios 端socketjs不能连接问题  

 

后台spring服务器代码如下

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    private static Logger logger= LoggerFactory.getLogger(WebSocketConfig.class);
    /**
     * 设置心跳的时间间隔
     */
    private final long HEART_BEAT=5000;

    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private RedisUtils redisUtils;
    @Value("${server.port}")
    private Integer port;
    @Autowired
    private MessageSend messageSend;

    /**
     * 设置stomp协议的入口(EndPoint)
     * 用于设置建立websocket连接
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        logger.info("用于设置连接websocket的入口地址,");
        //设置连接websocket的开始前缀。前台使用什么来建立websocket通信。ws://localhost:端口/ws-start
        registry.addEndpoint("/ws-start")
                .setAllowedOrigins("*")//表示允许跨域请求,即允许所有的来源请求
                .withSockJS();//使用SockJS进行通信(当浏览器不支持websocket,使用SockJS进行通信)
    }

前端js如下

/**
     * 建立连接
     */
        cc.error(">>>>>>>:" + "建立连接");
        // @ts-ignore
        const socket = new SockJS(`http://landlordapi.tiangu95.com/tiangugame/landlord/ws-start`, undefined, {transports: ['websocket']});
      
        this._stompCli = Stomp.over(socket);
        this._stompCli.heartbeat.outgoing = 2000;
        this._stompCli.heartbeat.incoming = 2000;
        let _that = this;
        this._stompCli.connect(
            {token: cc.sys.localStorage.getItem(DBK.ACCESS_TOKEN)},
            (frame) => {
                this.subscribeTopic();
                if (cc.director.getScene().name != ENUM_DDZ_SCENE.GAME) {
                    setTimeout(() => {
                        cc.director.loadScene(ENUM_DDZ_SCENE.GAME, () => {
                            this.sendEnterGame();
                        });
                    }, 1000);
                }
                cc.find("root/disTip").active = false;

                cc.error(">>>>>>>:" + "连接成功")
            },
            (error) => {
                cc.find("root/disTip").active = true;
                // this.doConnect();
            });
        // this._stompCli.debug = (str) => {
        // cc.log(">>>>>>::" + str);
        // }

解决办法找到SocketJs库查找到  this.xhr.withCredentials = true; 注释掉,解决

    // this.xhr.withCredentials = true; //修复ios下无法连接的问题

 

你可能感兴趣的:(socketJs,java)