Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can´t connect with Socket io to https with React Native

I am trying to setup my apps chat with socket.io and use https. It worked when i was using http, but since this was changed because of security i can not connect to my chat server anymore.

Actually there shouldn´t be much to do this, maybe anyone has experienced out similar issue?

Connecting to my Chat Server from my Web-Frontend works great, but trying this code below does not connect for Android as well as for 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);
    });
}

Using latest ReactNative and latest 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});
  }

........

Actually the only thing i have changed is updating socket io and react native to the latest and using UserAgent from ReactNative. I hope this help anyone

"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
like image 439
BigPun86 Avatar asked Nov 08 '22 12:11

BigPun86


1 Answers

Have you tried to place the certificate in your app? See this blog for more information: https://blog.synyx.de/2010/06/android-and-self-signed-ssl-certificates/

like image 168
mcliquid Avatar answered Nov 14 '22 21:11

mcliquid