SocketCluster / socketcluster-client

JavaScript client for SocketCluster
MIT License
291 stars 92 forks source link

React-Native connection error #102

Closed Ashu2245 closed 6 years ago

Ashu2245 commented 6 years ago

Hey , been trying to connect with socketcluster server from react native android , but now facing issue as

Socket hang Up,

My code and error are below

Client Code import socketCluster from '../../socketcluster.js' // Initiate the connection to the server var sampleChannel; var SocketConnection=''; this.Newsocket = socketCluster.connect({ hostname: '127.0.0.1', port: 80, secure:false, }); Worker Code `var SCWorker = require('socketcluster/scworker');

var SCChannel = require('sc-channel');

class Worker extends SCWorker { run() { console.log(' >> Worker PID:', process.pid); var environment = this.options.environment;

var httpServer = this.httpServer;
var scServer = this.scServer;

scServer.on('connection', function (socket) {
  console.log('client :'+ socket.id + ' connected.');
});

} }`

Server Code `/ This is the SocketCluster master controller file. It is responsible for bootstrapping the SocketCluster master process. Be careful when modifying the options object below. If you plan to run SCC on Kubernetes or another orchestrator at some point in the future, avoid changing the environment variable names below as each one has a specific meaning within the SC ecosystem. /

var fs = require('fs'); var argv = require('minimist')(process.argv.slice(2)); var scHotReboot = require('sc-hot-reboot'); var scErrors = require('sc-errors'); var TimeoutError = scErrors.TimeoutError;

var fsUtil = require('socketcluster/fsutil'); var waitForFile = fsUtil.waitForFile;

var SocketCluster = require('socketcluster');

var workerControllerPath = argv.wc || process.env.SOCKETCLUSTER_WORKER_CONTROLLER; var brokerControllerPath = argv.bc || process.env.SOCKETCLUSTER_BROKER_CONTROLLER; var workerClusterControllerPath = argv.wcc || process.env.SOCKETCLUSTER_WORKERCLUSTER_CONTROLLER; var environment = process.env.ENV || 'dev';

var options = { // Check other settings at https://socketcluster.io/#!/docs/api-socketcluster workers: Number(argv.w) || Number(process.env.SOCKETCLUSTER_WORKERS) || 1, brokers: Number(argv.b) || Number(process.env.SOCKETCLUSTER_BROKERS) || 1, host:'127.0.0.1', port: Number(argv.p) || Number(process.env.SOCKETCLUSTER_PORT) || 80, // If your system doesn't support 'uws', you can switch to 'ws' (which is slower but works on older systems). wsEngine: process.env.SOCKETCLUSTER_WS_ENGINE || 'uws', appName: argv.n || process.env.SOCKETCLUSTER_APP_NAME || 'SockCluster', workerController: workerControllerPath || dirname + '/worker.js', brokerController: brokerControllerPath || dirname + '/broker.js', workerClusterController: workerClusterControllerPath || null, socketChannelLimit: Number(process.env.SOCKETCLUSTER_SOCKET_CHANNEL_LIMIT) || 100, clusterStateServerHost: argv.cssh || process.env.SCC_STATE_SERVER_HOST || null, clusterStateServerPort: process.env.SCC_STATE_SERVER_PORT || null, clusterAuthKey: process.env.SCC_AUTH_KEY || null, clusterInstanceIp: process.env.SCC_INSTANCE_IP || null, clusterInstanceIpFamily: process.env.SCC_INSTANCE_IP_FAMILY || null, clusterStateServerConnectTimeout: Number(process.env.SCC_STATE_SERVER_CONNECT_TIMEOUT) || null, clusterStateServerAckTimeout: Number(process.env.SCC_STATE_SERVER_ACK_TIMEOUT) || null, clusterStateServerReconnectRandomness: Number(process.env.SCC_STATE_SERVER_RECONNECT_RANDOMNESS) || null, crashWorkerOnError: argv['auto-reboot'] != false, // If using nodemon, set this to true, and make sure that environment is 'dev'. killMasterOnSignal: false, environment: environment };

var bootTimeout = Number(process.env.SOCKETCLUSTER_CONTROLLER_BOOT_TIMEOUT) || 10000; var SOCKETCLUSTER_OPTIONS;

if (process.env.SOCKETCLUSTER_OPTIONS) { SOCKETCLUSTER_OPTIONS = JSON.parse(process.env.SOCKETCLUSTER_OPTIONS); }

for (var i in SOCKETCLUSTER_OPTIONS) { if (SOCKETCLUSTER_OPTIONS.hasOwnProperty(i)) { options[i] = SOCKETCLUSTER_OPTIONS[i]; } }

var start = function () { var socketCluster = new SocketCluster(options);

socketCluster.on(socketCluster.EVENT_WORKER_CLUSTER_START, function (workerClusterInfo) { console.log(' >> WorkerCluster PID:', workerClusterInfo.pid); });

if (socketCluster.options.environment == 'dev') { // This will cause SC workers to reboot when code changes anywhere in the app directory. // The second options argument here is passed directly to chokidar. // See https://github.com/paulmillr/chokidar#api for details. console.log(!! The sc-hot-reboot plugin is watching for code changes in the ${__dirname} directory); scHotReboot.attach(socketCluster, { cwd: __dirname, ignored: ['log','view','public/.js','node_modules', 'README.md', 'Dockerfile', 'server.js', 'broker.js', /[\/\]./, '.log'] }); } };

var bootCheckInterval = Number(process.env.SOCKETCLUSTER_BOOT_CHECK_INTERVAL) || 200; var bootStartTime = Date.now();

// Detect when Docker volumes are ready. var startWhenFileIsReady = (filePath) => { var errorMessage = Failed to locate a controller file at path ${filePath} + before SOCKETCLUSTER_CONTROLLER_BOOT_TIMEOUT;

return waitForFile(filePath, bootCheckInterval, bootStartTime, bootTimeout, errorMessage); };

var filesReadyPromises = [ startWhenFileIsReady(workerControllerPath), startWhenFileIsReady(brokerControllerPath), startWhenFileIsReady(workerClusterControllerPath) ]; Promise.all(filesReadyPromises) .then(() => { start(); }) .catch((err) => { console.error(err.stack); process.exit(1); });`

Server is running perfectly fine , and i can connect to server by browser, But with react native below error is showing 2018-02-07

and React native chrome Debugger is showing this

image

Can anyone please help me . Thanks

Ashu2245 commented 6 years ago

Hi, I got the solution of the above error. : )

The problem is I was using simulator to connect my localhost server..

But it's not going to be listening there unless you're running it on the device.

So after running the server remotely i am able to connect to the server from the simulator.

And other solution is If you want to send a request from the phone to a backend hosted locally You need 2 stuffs 1: Be on the same wifi/network 2: Use the ipv4 of the server computer when making your request So you don't use localhost:port You need http://server_computer_ipv4/:port