Socket.io-client: Kann keine Verbindung über socket.io-client auf React-Native herstellen

Erstellt am 31. Okt. 2018  ·  37Kommentare  ·  Quelle: socketio/socket.io-client

You want to: Du möchtest:

  • [x] report a bug [x] Fehler melden
  • [ ] request a feature [ ] eine Funktion anfordern

Current behaviour Aktuelles Verhalten

I can't connect via socket io to my server, it times out. Ich kann keine Verbindung über Socket io zu meinem Server herstellen, es kommt zu einer Zeitüberschreitung.

Steps to reproduce (if the current behaviour is a bug) Schritte zum Reproduzieren (wenn das aktuelle Verhalten ein Fehler ist)

In react-native: In React-Native:

import io from 'socket.io-client';


const BACKEND = 'my socketio server url'

const sock = io(BACKEND, { transports: ['websocket'], forceNew: true });
sock.on('connected', () => {
  debugger;
})

The debug logs look like: Die Debug-Logs sehen so aus:

socket.io-client:url parse <my url> +0ms
browser.js:133 socket.io-client ignoring socket cache for <my url> +0ms
browser.js:133 socket.io-client:manager readyState closed +0ms
browser.js:133 socket.io-client:manager opening <my url> +1ms
browser.js:133 engine.io-client:socket creating transport "websocket" +0ms
browser.js:133 engine.io-client:socket setting transport websocket +3ms
browser.js:133 socket.io-client:manager connect attempt will timeout after 20000 +6ms
browser.js:133 socket.io-client:manager readyState opening +2ms
socket.io-client:manager connect attempt timed out after 20000 +20s
browser.js:133 engine.io-client:socket socket close with reason: "forced close" +20s
browser.js:133 engine.io-client:socket socket closing - telling transport to close +2ms
browser.js:133 socket.io-client:manager connect_error +3ms
browser.js:133 socket.io-client:manager cleanup +1ms
browser.js:133 socket.io-client:manager will wait 899ms before reconnect attempt +1ms
browser.js:133 socket.io-client:manager attempting reconnect +912ms

Note : the best way (and by that we mean the only way ) to get a quick answer is to provide a failing test case by forking the following fiddle . Hinweis : Der beste Weg (und damit meinen wir den einzigen Weg ), um eine schnelle Antwort zu erhalten, besteht darin, einen fehlgeschlagenen Testfall bereitzustellen, indem Sie die folgende Geige forken .

Expected behaviour Erwartetes Verhalten

I expect to be able to connect to my socketio server, the same way I am able to in a web browser. Ich gehe davon aus, dass ich mich mit meinem Socketio-Server verbinden kann, genauso wie ich es in einem Webbrowser kann.

Setup Konfiguration

  • OS: iOS iPhone 10 simulator Betriebssystem: iOS iPhone 10-Simulator
  • browser: React-native Browser: React-nativ
  • socket.io version: 2.1.1 socket.io-Version: 2.1.1
en

Hilfreichster Kommentar

Finally found a solution for React Native >= 0.60 after a couple of coffees and digging deep into engine.io - additional to a version conflict (with RN) there seems to be an issue with passing in the URI. Endlich eine Lösung für React Native >= 0.60 nach ein paar Kaffees und tiefem Graben in engine.io gefunden - zusätzlich zu einem Versionskonflikt (mit RN) scheint es ein Problem mit der Übergabe des URI zu geben. When I pass an URI into socketIO, it gets turncated to the host and 'socket.io' appened. Wenn ich einen URI an socketIO übergebe, wird er an den Host übergeben und „socket.io“ angehängt. So socketIO("https://foo.bar/my/endpoint/socket.io") will lead to socketIO trying to connect to https://foo.bar/socket.io without showing it in the logs when throwing the xhr pull / socket error. socketIO("https://foo.bar/my/endpoint/socket.io") führt also dazu, dass socketIO versucht, sich mit https://foo.bar/socket.io zu verbinden, ohne es in den Protokollen anzuzeigen, wenn der xhr Pull / Socket-Fehler ausgelöst wird.

Here we go, how to get socket.io running on current versions of React Native : Los geht's, wie man socket.io auf aktuellen Versionen von React Native zum Laufen bringt :

  • use socket.io-client version _2.1.1_ Verwenden Sie socket.io-client Version _2.1.1_
// package.json
{
  "dependencies": {
     "socket.io-client": "2.1.1"
   }
}
  • when setting up the client, define a host and a path Definieren Sie beim Einrichten des Clients einen Host und einen Pfad
const socket = socketIO('https://your.host.com/', {
  path: '/path/to/socket.io/'
})
  • and suddenly everything works as smooth as planed und plötzlich läuft alles so glatt wie geplant

Hope to have saved you the time I spent on this issue 😆 Ich hoffe, Ihnen die Zeit erspart zu haben, die ich für dieses Thema aufgewendet habe 😆

en

Alle 37 Kommentare

Looks like this is react-native's issue. Sieht so aus, als wäre dies das Problem von React-Native. Earlier versions of RN seem to work with socket.io, but 0.0.57 does not. Frühere Versionen von RN scheinen mit socket.io zu funktionieren, 0.0.57 jedoch nicht. Closing. Schließen.

en

Try "socket.io-client": "2.0.4" Versuchen Sie "socket.io-client": "2.0.4"
This may be socket.io bug Dies kann ein Socket.io-Fehler sein

en

Try "socket.io-client": "2.0.4" Versuchen Sie "socket.io-client": "2.0.4"
This may be socket.io bug Dies kann ein Socket.io-Fehler sein

Yes it does work. Ja, es funktioniert.

en

Mit RN 0.52 und socket.io-client 2.0.4 stehe ich immer noch vor diesem Problem.

en

@noahprince22 can you please share your configuration ? @noahprince22 kannst du bitte deine Konfiguration teilen?
I am currently using this and the issue still persist. Ich benutze es derzeit und das Problem besteht weiterhin.

"react-native": "0.55.4",
"react": "16.3.1",
"socket.io-client": "^2.0.4",
"socket.io: "^2.2.0" //server side
en

any workaround? irgendeine Problemumgehung?

socket.io team please, this should work with RN, is a socket.io bug. socket.io-Team bitte, das sollte mit RN funktionieren, ist ein Socket.io-Bug.

en

Hi @ningacoding , I managed to get it working with this configuration Hallo @ningacoding , ich habe es geschafft, es mit dieser Konfiguration zum Laufen zu bringen

"react-native": "0.55.4",
"react": "16.3.1",
"socket.io-client": "^2.0.4",
"socket.io: "^1.0.0" //server side

In your react-native app use this Verwenden Sie dies in Ihrer React-Native-App

const socket = io(BASE_URL, {
    secure: true,
    transports: ['websocket'],
  });
en

"react-native": "0.58.6", "react-native": "0.58.6",
"react": "16.6.3", "reagieren": "16.6.3",
"socket.io-client": "^2.0.4", "socket.io-client": "^2.0.4",

Not working for me Funktioniert nicht für mich

en

any solution? irgendeine Lösungsmöglichkeit? I'm facing the same thing Ich stehe vor dem gleichen

en

I have long since stopped working on the project that I was having this issue with. Ich habe schon lange aufgehört, an dem Projekt zu arbeiten, mit dem ich dieses Problem hatte. If I remember correctly, I downgraded react-native to "solve' the problem. Wenn ich mich richtig erinnere, habe ich React-Native heruntergestuft, um das Problem zu "lösen".

en

Experiencing the same issue, has there been a fix yet? Habe das gleiche Problem, gab es schon eine Lösung? Anyone find any workarounds to implementing sockets in React Native? Findet jemand Workarounds für die Implementierung von Sockets in React Native?

en

@russellgoldman I had problems with RN 0.60.5 and socket.io. @russellgoldman Ich hatte Probleme mit RN 0.60.5 und socket.io. Release build did not connect to the server. Release-Build hat keine Verbindung zum Server hergestellt.

None of socket.io versions worked with RN 0.60.5: I tried versions 2.0.4, 2.1.1, 2.2.0. Keine der socket.io-Versionen funktionierte mit RN 0.60.5: Ich habe die Versionen 2.0.4, 2.1.1, 2.2.0 ausprobiert.

Downgrading to RN 0.59.10 solved my problem. Ein Downgrade auf RN 0.59.10 hat mein Problem gelöst.

en

Hi @anttirauh and @russellgoldman , Hallo @anttirauh und @russellgoldman ,
I tried with RN 0.59.10 and Socket 2.0.4, but its not connecting on release build. Ich habe es mit RN 0.59.10 und Socket 2.0.4 versucht, aber es verbindet sich nicht beim Release-Build.
Can you please help me? Kannst du mir bitte helfen?

en

@ Ramesh21071993 Ich denke, Sie müssen socket.io auf Version 2.1.1 aktualisieren.

en

@anttirauh , i tried with all these versions socket io (2.0.4, 2.1.1, 2.2.0) and react-native versions (0.60, 0.59.10, 0.59.4) @anttirauh , ich habe es mit all diesen Versionen versucht, Socket io (2.0.4, 2.1.1, 2.2.0) und React-native-Versionen (0.60, 0.59.10, 0.59.4)
In server side also socket io 2.0.4. Serverseitig auch Socket io 2.0.4.
still that same issue sir, Immer noch das gleiche Problem, Sir,

But it works on RN 0.55.4 Aber es funktioniert auf RN 0.55.4

en

@Ramesh21071993 My understanding (which is formed with heavy googling and own testing) is that: @ Ramesh21071993 Mein Verständnis (das durch intensives Googeln und eigene Tests gebildet wird) ist Folgendes:

  • socket.io stopped working with react-native about around RN version 0.57.4 socket.io funktioniert nicht mehr mit React-Native um die RN-Version 0.57.4 herum
  • socket.io or some other component was fixed later and I managed to get it working with version combo: RN 0.59.10 + socket.io 2.1.1 socket.io oder eine andere Komponente wurde später behoben und ich habe es geschafft, es mit der Versionskombination zum Laufen zu bringen: RN 0.59.10 + socket.io 2.1.1
  • RN version 0.60.x is not working with any version of socket.io RN-Version 0.60.x funktioniert mit keiner Version von socket.io

So my advice is only: try one time more with RN 0.59.10 and socket.io 2.1.1 and make sure that you clear build folder before building. Also mein Rat ist nur: Probieren Sie es noch einmal mit RN 0.59.10 und socket.io 2.1.1 und stellen Sie sicher, dass Sie den Build-Ordner vor dem Bauen leeren. And remove node_modules before yarn/npm. Und entfernen Sie node_modules vor Garn/npm.

en

@anttirauh Sir, below is my package.json. @anttirauh Sir, unten ist meine package.json. I did as what you told. Ich habe getan, was du gesagt hast. But nothing happen. Aber nichts passiert. I remove build folder and node modules and again i install. Ich entferne Build-Ordner und Knotenmodule und installiere erneut.
Please help me on this to work, Past one month am trying Bitte helfen Sie mir, daran zu arbeiten. Nach einem Monat versuche ich es

App package.json App-Paket.json

{ {
"name": "Track", "name": "Track",
"version": "0.0.1", "Version": "0.0.1",
"private": true, "privat": wahr,
"scripts": { "Skripte": {
"start": "node node_modules/react-native/local-cli/cli.js start", "start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest" "test": "Scherz"
}, },
"dependencies": { "Abhängigkeiten": {
"react": "16.8.3", "reagieren": "16.8.3",
"react-native": "0.59.10", "react-native": "0.59.10",
"socket.io": "2.1.1" "socket.io": "2.1.1"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.6.2", "@babel/core": "^7.6.2",
"@babel/runtime": "^7.6.2", "@babel/runtime": "^7.6.2",
"babel-jest": "^24.9.0", "babel-scherz": "^24.9.0",
"jest": "^24.9.0", "Scherz": "^24.9.0",
"metro-react-native-babel-preset": "^0.56.0", "metro-react-native-babel-preset": "^0.56.0",
"react-test-renderer": "16.8.3" "react-test-renderer": "16.8.3"
}, },
"jest": { "Scherz": {
"preset": "react-native" "preset": "react-native"
} }
} }

Server Package.json Serverpaket.json

{ {
"name": "TRACK-Socket", "name": "TRACK-Socket",
"version": "0.0.1", "Version": "0.0.1",
"dependencies": { "Abhängigkeiten": {
"amqplib": "^0.5.1", "amqplib": "^0.5.1",
"config": "^3.2.2", "config": "^3.2.2",
"express": "^4.15.3", "express": "^4.15.3",
"socket.io": "^2.1.1", "socket.io": "^2.1.1",
"ws": "^7.1.2" "ws": "^7.1.2"
} }
} }

Thanks Vielen Dank

en

Finally found a solution for React Native >= 0.60 after a couple of coffees and digging deep into engine.io - additional to a version conflict (with RN) there seems to be an issue with passing in the URI. Endlich eine Lösung für React Native >= 0.60 nach ein paar Kaffees und tiefem Graben in engine.io gefunden - zusätzlich zu einem Versionskonflikt (mit RN) scheint es ein Problem mit der Übergabe des URI zu geben. When I pass an URI into socketIO, it gets turncated to the host and 'socket.io' appened. Wenn ich einen URI an socketIO übergebe, wird er an den Host übergeben und „socket.io“ angehängt. So socketIO("https://foo.bar/my/endpoint/socket.io") will lead to socketIO trying to connect to https://foo.bar/socket.io without showing it in the logs when throwing the xhr pull / socket error. socketIO("https://foo.bar/my/endpoint/socket.io") führt also dazu, dass socketIO versucht, sich mit https://foo.bar/socket.io zu verbinden, ohne es in den Protokollen anzuzeigen, wenn der xhr Pull / Socket-Fehler ausgelöst wird.

Here we go, how to get socket.io running on current versions of React Native : Los geht's, wie man socket.io auf aktuellen Versionen von React Native zum Laufen bringt :

  • use socket.io-client version _2.1.1_ Verwenden Sie socket.io-client Version _2.1.1_
// package.json
{
  "dependencies": {
     "socket.io-client": "2.1.1"
   }
}
  • when setting up the client, define a host and a path Definieren Sie beim Einrichten des Clients einen Host und einen Pfad
const socket = socketIO('https://your.host.com/', {
  path: '/path/to/socket.io/'
})
  • and suddenly everything works as smooth as planed und plötzlich läuft alles so glatt wie geplant

Hope to have saved you the time I spent on this issue 😆 Ich hoffe, Ihnen die Zeit erspart zu haben, die ich für dieses Thema aufgewendet habe 😆

en

@rastapasta , i got an error like “ CLEARTEXT communication not permitted by network security policy ”, @rastapasta , ich habe eine Fehlermeldung wie „ CLEARTEXT-Kommunikation durch Netzwerksicherheitsrichtlinie nicht zulässig “ erhalten.

Please tell me what to do Bitte sag mir was zu tun ist

en

@Ramesh21071993 if you can't use your endpoint via https (please consider it), add their hostnames to the exception lists. @Ramesh21071993 Wenn Sie Ihren Endpunkt nicht über https verwenden können (bitte berücksichtigen Sie dies), fügen Sie deren Hostnamen zu den Ausnahmelisten hinzu.

for iOS: https://stackoverflow.com/a/30732693 für iOS: https://stackoverflow.com/a/30732693
for Android: https://stackoverflow.com/a/50834600 für Android: https://stackoverflow.com/a/50834600

en

Sir, I tried that also but not able to do. Sir, das habe ich auch versucht, aber nicht geschafft. If you don't mind can you please give me any demo source files Wenn es Ihnen nichts ausmacht, können Sie mir bitte Demo-Quelldateien geben
For long time I struck with this. Lange habe ich damit geschlagen.

Am using ip and port sir Benutze IP und Port Sir

en

Befolgen Sie die Anweisungen der obigen Dokumentationen und passen Sie diese Implementierung an:

import socketIO from 'socket.io-client'
const socket = socketIO('http://8.8.8.8:3000/', {
  path: '/your/path/to/socket.io/'
  reconnectionDelay: 1000,
  reconnection: true,
  reconnectionAttempts: Infinity,
  jsonp: false
})

socket.on('connect', () => console.log('connected'))
socket.on('error', console.error)
socket.on('connect_error', console.error)
en

Sir, auf der Serverseite verwende ich Socket io 2.1.1, ist es in Ordnung oder muss etwas geändert werden?

en

image

en

@Lizhooh hackige Art, Whazzup auf rn herauszufinden: Monkey patch ein console.log(err) vor dem self.onError in Ihrer node_modules engine.io-client/blob/master/lib/transports/ polling-xhr.js #L131

en

@Ramesh21071993 Is your issue resolved? @Ramesh21071993 Ist Ihr Problem gelöst? I am facing the same issue. Ich stehe vor dem gleichen Problem. Tried everything mentioned in above comments. Habe alles versucht, was in den obigen Kommentaren erwähnt wurde.

en

@rastapasta I followed what you have suggested above. @rastapasta Ich bin dem gefolgt, was Sie oben vorgeschlagen haben. My socket connection happens and then immediately disconnects. Meine Socket-Verbindung passiert und wird dann sofort getrennt. I get timeout error infinitely. Ich erhalte unendlich Zeitüberschreitungsfehler. However, whenever I click on some component which makes a network call, the socket connects and immediately disconnects. Immer wenn ich jedoch auf eine Komponente klicke, die einen Netzwerkanruf tätigt, verbindet sich der Socket und wird sofort wieder getrennt. If I don't do any network call after first socket disconnect, it would stay in the same state. Wenn ich nach dem ersten Trennen des Sockets keinen Netzwerkanruf tätige, bleibt es im selben Zustand. Please, could you help? Bitte, könnten Sie helfen?

socket = io('http://IP:31515', { socket = io('http://IP:31515', {
path: '/socketio', //I have given this is the path in my socket server as well as my kubernetes ingress file to route to that port path: '/socketio', // Ich habe angegeben, dass dies der Pfad in meinem Socket-Server sowie meine Kubernetes-Ingress-Datei ist, um zu diesem Port zu leiten
transports: ['websocket'], Transporte: ['websocket'],
reconnectionDelay: 1000, Wiederverbindungsverzögerung: 1000,
reconnection: true, Wiederverbindung: wahr,
reconnectionAttempts: Infinity, Wiederverbindungsversuche: Unendlich,
// timeout: 30000, // Zeitüberschreitung: 30000,
jsonp: false jsonp: falsch
}); });

things that I have tried: Dinge, die ich versucht habe:

  1. removing and adding transports option Entfernen und Hinzufügen von Transportoptionen
    2.removing and add timeout 2. Zeitüberschreitung entfernen und hinzufügen
  2. removing all parameters except path Entfernen aller Parameter außer Pfad
  3. adding pingTimeout and pingInterval in server socket configuration Hinzufügen von pingTimeout und pingInterval in der Server-Socket-Konfiguration
  4. removing path from server and client and only keeping it in my kubernetes ingress file to route to that deployment/pod. Pfad von Server und Client entfernen und ihn nur in meiner Kubernetes-Ingress-Datei behalten, um zu dieser Bereitstellung/diesem Pod zu leiten.

Note: the same server and kubernetes conf work for any node.js socket client as well as my react js web app. Hinweis: Der gleiche Server und die gleiche Kubernetes-Konfiguration funktionieren für jeden node.js-Socket-Client sowie für meine React-Js-Web-App. I am only facing this issue for React Native app in debug as well as release mode. Ich habe dieses Problem nur für die React Native-App im Debug- und Release-Modus.

en

Hat jemand eine Idee zu meinem oben genannten Problem?

en

@calqlum123 @calqlum123
Please make sure to Bitte achten Sie darauf

  • add your host to the transport exception list (s. here ) Fügen Sie Ihren Host zur Transport-Ausnahmeliste hinzu (s. hier )
  • try it without manually defining the transports versuchen Sie es, ohne die Transporte manuell zu definieren
  • monkey patch the console.log (s. here ) and check the error monkey patcht das console.log (s. hier ) und prüft den Fehler
en

Can anyone please share a complete working example for both client and server? Kann jemand bitte ein vollständiges Arbeitsbeispiel für Client und Server teilen? Because this is confusing. Denn das ist verwirrend.

en

I am using "react-native": "0.61.5" and "socket.io-client": "^2.1.1", Ich verwende "react-native": "0.61.5" und "socket.io-client": "^2.1.1",

below is my code Unten ist mein Code

const socket = SocketIOClient('http://192.168.0.101:1234',{reconnect: true}); socket.connect(); socket.once("connect", () => { console.log("[CLIENT] Connected to remote server, identificating."); // this.sendIdentification(); }); socket.on('connect', () => { console.log('connected to socket server'); });

The code is not able to connect to the server and I am also looking for the solution. Der Code kann keine Verbindung zum Server herstellen und ich suche auch nach der Lösung. I don't want to downgrade my react-native version. Ich möchte meine React-Native-Version nicht herabstufen.

en

you must add this code in application tag Sie müssen diesen Code im Anwendungs-Tag hinzufügen
like below in AndroidManifest.xml wie unten in AndroidManifest.xml
. . . . . .

en

I am using "react-native": "0.61.5" and "socket.io-client": "^2.1.1", Ich verwende "react-native": "0.61.5" und "socket.io-client": "^2.1.1",

below is my code Unten ist mein Code

const socket = SocketIOClient('http://192.168.0.101:1234',{reconnect: true}); socket.connect(); socket.once("connect", () => { console.log("[CLIENT] Connected to remote server, identificating."); // this.sendIdentification(); }); socket.on('connect', () => { console.log('connected to socket server'); });

The code is not able to connect to the server and I am also looking for the solution. Der Code kann keine Verbindung zum Server herstellen und ich suche auch nach der Lösung. I don't want to downgrade my react-native version. Ich möchte meine React-Native-Version nicht herabstufen.

Can you try "ws://192.168.0.101:1234", instead of " http://192.168.0.101 :1234" Können Sie "ws://192.168.0.101:1234" anstelle von " http://192.168.0.101:1234 " versuchen

en

@Unforgiven-wanda ein Beispiel mit React Native wurde hier hinzugefügt: https://github.com/socketio/socket.io/tree/master/examples/react-native

en

Keine Verbindung mit „socket.io-client“ möglich: „^3.0.4“ in React Native

en

@chiragpurohit71085 have you checked the example there: https://github.com/socketio/socket.io/tree/master/examples/react-native? @chiragpurohit71085 hast du das Beispiel dort überprüft: https://github.com/socketio/socket.io/tree/master/examples/react-native?

I think that this is a problem of URL, could you please check if your server is reachable at http://192.168.x.y:z ? Ich denke, dass dies ein URL-Problem ist. Könnten Sie bitte überprüfen, ob Ihr Server unter http://192.168.x.y:z erreichbar ist?

en
War diese Seite hilfreich?
0 / 5 - 0 Bewertungen