javascript – 无法使用React Native与Socket io连接到https

我正在尝试使用socket.io设置我的应用程序聊天并使用https.它在我使用http时工作,但由于安全性而改变了我无法连接到我的聊天服务器.

实际上应该没什么可做的,也许任何人都遇到过类似的问题?

从我的Web-Frontend连接到我的聊天服务器效果很好,但尝试下面的代码不能连接Android和iOS:

import './UserAgent.js';
import io from 'socket.io-client/socket.io';

const connectionOptions = {
  jsonp     : false,
  transports: ['websocket'],
  secure    : true
};

export class App extends Component {

  constructor() {
    super();
    this.state = {
        chatThings: ''
    };

    this.chatUrl = 'https://chat.foo.info';
    this.socket  = io(this.chatUrl, connectionOptions)
  }

  connectToChatServer() {

    let tries = 0;
    var that  = this;
    setInterval(()=> {
        that.socket.connect(that.chatUrl, connectionOptions);
        console.log('CHAT url', that.chatUrl);
        if (that.socket.connected) {
            alert('Finally CONNECTED!!!!!!');
            that.setState({
                connected: socket.connected
            });

        }
        tries = tries + 1;
    }, 2500);

    this.socket.on('error', (err)=> {
        console.log('CHAT: error', err);
    });
}

使用最新的ReactNative和最新的socket.io

UPDATE

window.navigator.userAgent = 'ReactNative';

var io = require('../node_modules/socket.io-client/dist/socket.io');

// -----------------------------------------------------------------------------------------------------------------
// Chat
// -----------------------------------------------------------------------------------------------------------------

Backend.prototype.connectToChatServer = function () {
  let self = this;
  this.dispatch(Actions.connectToChatServer());

  const connectionOptions = {
    jsonp     : false,
    secure    : true,
    transports: ['websocket']
  };

  log.info('CHAT IO CONNECTION');
  this.socket = io(this.chatUrl, connectionOptions);
  function authenticate() {
    self.socket.emit('authenticate', {token: Store.getState().User.token});
  }

........

实际上我唯一改变的就是更新socket io并使用ReactNative中的UserAgent使用最新版本并使用UserAgent.我希望这有助于任何人

"socket.io-client": "^1.7.1",
"react-native": "^0.29.2", // this version worked, i have tested it on RN 43 too which works as well

最佳答案 您是否尝试将证书放入应用程序?

有关更多信息,请参阅此博客:
https://blog.synyx.de/2010/06/android-and-self-signed-ssl-certificates/

点赞