Socket.io-client: Tidak dapat terhubung melalui socket.io-client di react-native

Dibuat pada 31 Okt 2018  ·  37Komentar  ·  Sumber: socketio/socket.io-client

You want to: Yang kamu ingin:

  • [x] report a bug [x] laporkan bug
  • [ ] request a feature [ ] meminta fitur

Current behaviour Perilaku saat ini

I can't connect via socket io to my server, it times out. Saya tidak dapat terhubung melalui socket io ke server saya, waktu habis.

Steps to reproduce (if the current behaviour is a bug) Langkah-langkah untuk mereproduksi (jika perilaku saat ini adalah bug)

In react-native: Dalam reaksi-asli:

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: Log debug terlihat seperti:

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 . Catatan : cara terbaik (dan maksud kami satu-satunya cara ) untuk mendapatkan jawaban cepat adalah dengan memberikan kasus uji yang gagal dengan forking biola berikut.

Expected behaviour Perilaku yang diharapkan

I expect to be able to connect to my socketio server, the same way I am able to in a web browser. Saya berharap dapat terhubung ke server socketio saya, dengan cara yang sama seperti yang saya lakukan di browser web.

Setup Mendirikan

  • OS: iOS iPhone 10 simulator OS: simulator iOS iPhone 10
  • browser: React-native browser: React-asli
  • socket.io version: 2.1.1 versi socket.io: 2.1.1
en

Komentar yang paling membantu

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. Akhirnya menemukan solusi untuk React Native >= 0.60 setelah beberapa kopi dan menggali jauh ke dalam engine.io - tambahan untuk konflik versi (dengan RN) tampaknya ada masalah dengan meneruskan URI. When I pass an URI into socketIO, it gets turncated to the host and 'socket.io' appened. Ketika saya meneruskan URI ke socketIO, itu dialihkan ke Host dan 'socket.io' ditambahkan. 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. Jadi socketIO("https://foo.bar/my/endpoint/socket.io") akan menyebabkan socketIO mencoba terhubung ke https://foo.bar/socket.io tanpa menunjukkannya di log saat melempar kesalahan tarik / soket xhr.

Here we go, how to get socket.io running on current versions of React Native : Ini dia, cara menjalankan socket.io pada versi React Native saat ini :

  • use socket.io-client version _2.1.1_ gunakan socket.io-client versi _2.1.1_
// package.json
{
  "dependencies": {
     "socket.io-client": "2.1.1"
   }
}
  • when setting up the client, define a host and a path saat menyiapkan klien, tentukan host dan jalur
const socket = socketIO('https://your.host.com/', {
  path: '/path/to/socket.io/'
})
  • and suddenly everything works as smooth as planed dan tiba-tiba semuanya bekerja semulus yang direncanakan

Hope to have saved you the time I spent on this issue 😆 Berharap telah menghemat waktu yang saya habiskan untuk masalah ini

en

Semua 37 komentar

Looks like this is react-native's issue. Sepertinya ini adalah masalah reaksi-asli. Earlier versions of RN seem to work with socket.io, but 0.0.57 does not. Versi RN sebelumnya tampaknya berfungsi dengan socket.io, tetapi 0.0.57 tidak. Closing. Penutupan.

en

Try "socket.io-client": "2.0.4" Coba "socket.io-client": "2.0.4"
This may be socket.io bug Ini mungkin bug socket.io

en

Try "socket.io-client": "2.0.4" Coba "socket.io-client": "2.0.4"
This may be socket.io bug Ini mungkin bug socket.io

Yes it does work. Ya itu tidak bekerja.

en

Dengan RN 0.52 dan socket.io-client 2.0.4 saya masih menghadapi masalah ini.

en

@noahprince22 can you please share your configuration ? @noahprince22 dapatkah Anda membagikan konfigurasi Anda?
I am currently using this and the issue still persist. Saat ini saya menggunakan ini dan masalahnya masih ada.

"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? ada solusi?

socket.io team please, this should work with RN, is a socket.io bug. tim socket.io tolong, ini harus bekerja dengan RN, adalah bug socket.io.

en

Hi @ningacoding , I managed to get it working with this configuration Hai @ningacoding , saya berhasil membuatnya bekerja dengan konfigurasi ini

"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 Di aplikasi asli reaksi Anda, gunakan ini

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

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

Not working for me Tidak bekerja untuk saya

en

any solution? ada solusi? I'm facing the same thing Saya menghadapi hal yang sama

en

I have long since stopped working on the project that I was having this issue with. Saya sudah lama berhenti mengerjakan proyek yang bermasalah dengan saya. If I remember correctly, I downgraded react-native to "solve' the problem. Jika saya ingat dengan benar, saya menurunkan versi reaksi asli menjadi "menyelesaikan' masalah.

en

Experiencing the same issue, has there been a fix yet? Mengalami masalah yang sama, apakah sudah ada perbaikan? Anyone find any workarounds to implementing sockets in React Native? Adakah yang menemukan solusi untuk mengimplementasikan soket di React Native?

en

@russellgoldman I had problems with RN 0.60.5 and socket.io. @russellgoldman saya punya masalah dengan RN 0.60.5 dan socket.io. Release build did not connect to the server. Rilis build tidak terhubung ke server.

None of socket.io versions worked with RN 0.60.5: I tried versions 2.0.4, 2.1.1, 2.2.0. Tidak ada versi socket.io yang bekerja dengan RN 0.60.5: Saya mencoba versi 2.0.4, 2.1.1, 2.2.0.

Downgrading to RN 0.59.10 solved my problem. Menurunkan ke RN 0.59.10 memecahkan masalah saya.

en

Hi @anttirauh and @russellgoldman , Hai @anttirauh dan @russellgoldman ,
I tried with RN 0.59.10 and Socket 2.0.4, but its not connecting on release build. Saya mencoba dengan RN 0.59.10 dan Socket 2.0.4, tetapi tidak terhubung pada rilis build.
Can you please help me? Bisakah kamu menolongku?

en

@ Ramesh21071993 Saya pikir Anda harus memutakhirkan socket.io ke versi 2.1.1.

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 , saya mencoba dengan semua versi ini socket io (2.0.4, 2.1.1, 2.2.0) dan versi asli-reaksi (0.60, 0.59.10, 0.59.4)
In server side also socket io 2.0.4. Di sisi server juga soket io 2.0.4.
still that same issue sir, masih masalah yang sama pak,

But it works on RN 0.55.4 Tapi itu bekerja pada RN 0.55.4

en

@Ramesh21071993 My understanding (which is formed with heavy googling and own testing) is that: @ Ramesh21071993 Pemahaman saya (yang dibentuk dengan googling berat dan pengujian sendiri) adalah bahwa:

  • socket.io stopped working with react-native about around RN version 0.57.4 socket.io berhenti bekerja dengan reaksi asli sekitar RN versi 0.57.4
  • 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 atau komponen lain diperbaiki nanti dan saya berhasil membuatnya bekerja dengan versi kombo: RN 0.59.10 + socket.io 2.1.1
  • RN version 0.60.x is not working with any version of socket.io RN versi 0.60.x tidak berfungsi dengan versi socket.io apa pun

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. Jadi saran saya hanya: coba sekali lagi dengan RN 0.59.10 dan socket.io 2.1.1 dan pastikan Anda menghapus folder build sebelum membangun. And remove node_modules before yarn/npm. Dan hapus node_modules sebelum yarn/npm.

en

@anttirauh Sir, below is my package.json. @anttirauh Pak, di bawah ini adalah package.json saya. I did as what you told. Saya melakukan apa yang Anda katakan. But nothing happen. Tapi tidak ada yang terjadi. I remove build folder and node modules and again i install. Saya menghapus folder build dan modul node dan sekali lagi saya menginstal.
Please help me on this to work, Past one month am trying Tolong bantu saya agar ini berhasil, Satu bulan terakhir saya mencoba

App package.json Paket aplikasi.json

{ {
"name": "Track", "nama": "Lacak",
"version": "0.0.1", "versi": "0.0.1",
"private": true, "pribadi": benar,
"scripts": { "skrip": {
"start": "node node_modules/react-native/local-cli/cli.js start", "mulai": "simpul node_modules/react-native/local-cli/cli.js mulai",
"test": "jest" "tes": "bercanda"
}, },
"dependencies": { "ketergantungan": {
"react": "16.8.3", "bereaksi": "16.8.3",
"react-native": "0.59.10", "react-asli": "0.59.10",
"socket.io": "2.1.1" "socket.io": "2.1.1"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.6.2", "@babel/inti": "^7.6.2",
"@babel/runtime": "^7.6.2", "@babel/runtime": "^7.6.2",
"babel-jest": "^24.9.0", "babel-jest": "^24.9.0",
"jest": "^24.9.0", "bercanda": "^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": { "bercanda": {
"preset": "react-native" "preset": "asli-reaksi"
} }
} }

Server Package.json Paket Server.json

{ {
"name": "TRACK-Socket", "nama": "Socket-TRACK",
"version": "0.0.1", "versi": "0.0.1",
"dependencies": { "ketergantungan": {
"amqplib": "^0.5.1", "amqplib": "^0.5.1",
"config": "^3.2.2", "konfigurasi": "^3.2.2",
"express": "^4.15.3", "ekspres": "^4.15.3",
"socket.io": "^2.1.1", "socket.io": "^2.1.1",
"ws": "^7.1.2" "ws": "^7.1.2"
} }
} }

Thanks terima kasih

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. Akhirnya menemukan solusi untuk React Native >= 0.60 setelah beberapa kopi dan menggali jauh ke dalam engine.io - tambahan untuk konflik versi (dengan RN) tampaknya ada masalah dengan meneruskan URI. When I pass an URI into socketIO, it gets turncated to the host and 'socket.io' appened. Ketika saya meneruskan URI ke socketIO, itu dialihkan ke Host dan 'socket.io' ditambahkan. 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. Jadi socketIO("https://foo.bar/my/endpoint/socket.io") akan menyebabkan socketIO mencoba terhubung ke https://foo.bar/socket.io tanpa menunjukkannya di log saat melempar kesalahan tarik / soket xhr.

Here we go, how to get socket.io running on current versions of React Native : Ini dia, cara menjalankan socket.io pada versi React Native saat ini :

  • use socket.io-client version _2.1.1_ gunakan socket.io-client versi _2.1.1_
// package.json
{
  "dependencies": {
     "socket.io-client": "2.1.1"
   }
}
  • when setting up the client, define a host and a path saat menyiapkan klien, tentukan host dan jalur
const socket = socketIO('https://your.host.com/', {
  path: '/path/to/socket.io/'
})
  • and suddenly everything works as smooth as planed dan tiba-tiba semuanya bekerja semulus yang direncanakan

Hope to have saved you the time I spent on this issue 😆 Berharap telah menghemat waktu yang saya habiskan untuk masalah ini

en

@rastapasta , i got an error like “ CLEARTEXT communication not permitted by network security policy ”, @rastapasta , saya mendapat kesalahan seperti " komunikasi CLEARTEXT tidak diizinkan oleh kebijakan keamanan jaringan ",

Please tell me what to do Tolong beri tahu saya apa yang harus dilakukan

en

@Ramesh21071993 if you can't use your endpoint via https (please consider it), add their hostnames to the exception lists. @ Ramesh21071993 jika Anda tidak dapat menggunakan titik akhir Anda melalui https (harap pertimbangkan), tambahkan nama host mereka ke daftar pengecualian.

for iOS: https://stackoverflow.com/a/30732693 untuk iOS: https://stackoverflow.com/a/30732693
for Android: https://stackoverflow.com/a/50834600 untuk Android: https://stackoverflow.com/a/50834600

en

Sir, I tried that also but not able to do. Pak, saya juga mencoba itu tetapi tidak bisa. If you don't mind can you please give me any demo source files Jika Anda tidak keberatan, bisakah Anda memberi saya file sumber demo?
For long time I struck with this. Untuk waktu yang lama saya memukul dengan ini.

Am using ip and port sir Saya menggunakan ip dan port pak

en

ikuti petunjuk dokumentasi di atas dan sesuaikan implementasi ini:

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

Pak, di sisi server saya menggunakan soket io 2.1.1, apakah boleh atau ada yang perlu diubah?

en

image

en

@Lizhooh cara hacky untuk mengetahui whazzup di rn: monkey menambal console.log(err) sebelum self.onError di node_modules Anda engine.io-client/blob/master/lib/transports/ polling-xhr.js #L131

en

@Ramesh21071993 Is your issue resolved? @ Ramesh21071993 Apakah masalah Anda teratasi? I am facing the same issue. Saya menghadapi masalah yang sama. Tried everything mentioned in above comments. Mencoba semua yang disebutkan dalam komentar di atas.

en

@rastapasta I followed what you have suggested above. @rastapasta Saya mengikuti apa yang Anda sarankan di atas. My socket connection happens and then immediately disconnects. Koneksi soket saya terjadi dan kemudian segera terputus. I get timeout error infinitely. Saya mendapatkan kesalahan batas waktu tanpa batas. However, whenever I click on some component which makes a network call, the socket connects and immediately disconnects. Namun, setiap kali saya mengklik beberapa komponen yang membuat panggilan jaringan, soket terhubung dan segera terputus. If I don't do any network call after first socket disconnect, it would stay in the same state. Jika saya tidak melakukan panggilan jaringan apa pun setelah pemutusan soket pertama, itu akan tetap dalam kondisi yang sama. Please, could you help? Tolong, bisakah Anda membantu?

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', //Saya telah memberikan ini adalah path di server socket saya serta file ingress kubernetes saya untuk merutekan ke port itu
transports: ['websocket'], mengangkut: ['websocket'],
reconnectionDelay: 1000, rekoneksiDelay: 1000,
reconnection: true, rekoneksi: benar,
reconnectionAttempts: Infinity, upaya penyambungan kembali: Tak terhingga,
// timeout: 30000, // batas waktu: 30000,
jsonp: false jsonp: salah
}); });

things that I have tried: hal-hal yang telah saya coba:

  1. removing and adding transports option menghapus dan menambahkan opsi transportasi
    2.removing and add timeout 2.menghapus dan menambahkan batas waktu
  2. removing all parameters except path menghapus semua parameter kecuali jalur
  3. adding pingTimeout and pingInterval in server socket configuration menambahkan pingTimeout dan pingInterval dalam konfigurasi soket server
  4. removing path from server and client and only keeping it in my kubernetes ingress file to route to that deployment/pod. menghapus jalur dari server dan klien dan hanya menyimpannya di file masuk kubernetes saya untuk merutekan ke penyebaran/pod itu.

Note: the same server and kubernetes conf work for any node.js socket client as well as my react js web app. Catatan: server dan conf kubernetes yang sama berfungsi untuk semua klien socket node.js serta aplikasi web react js saya. I am only facing this issue for React Native app in debug as well as release mode. Saya hanya menghadapi masalah ini untuk aplikasi React Native dalam mode debug dan rilis.

en

Apakah ada yang punya ide untuk masalah saya di atas?

en

@calqlum123 @calqlum123
Please make sure to Harap pastikan untuk

  • add your host to the transport exception list (s. here ) tambahkan Host Anda ke daftar pengecualian transportasi (s. di sini )
  • try it without manually defining the transports coba tanpa mendefinisikan transport secara manual
  • monkey patch the console.log (s. here ) and check the error monkey patch the console.log (s. here ) dan periksa kesalahannya
en

Can anyone please share a complete working example for both client and server? Adakah yang bisa berbagi contoh kerja lengkap untuk klien dan server? Because this is confusing. Karena ini membingungkan.

en

I am using "react-native": "0.61.5" and "socket.io-client": "^2.1.1", Saya menggunakan "react-native": "0.61.5" dan "socket.io-client": "^2.1.1",

below is my code di bawah ini adalah kode saya

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. Kode tidak dapat terhubung ke server dan saya juga mencari solusinya. I don't want to downgrade my react-native version. Saya tidak ingin menurunkan versi reaksi asli saya.

en

you must add this code in application tag Anda harus menambahkan kode ini di tag aplikasi
like below in AndroidManifest.xml seperti di bawah ini di AndroidManifest.xml
. . . . . .

en

I am using "react-native": "0.61.5" and "socket.io-client": "^2.1.1", Saya menggunakan "react-native": "0.61.5" dan "socket.io-client": "^2.1.1",

below is my code di bawah ini adalah kode saya

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. Kode tidak dapat terhubung ke server dan saya juga mencari solusinya. I don't want to downgrade my react-native version. Saya tidak ingin menurunkan versi reaksi asli saya.

Can you try "ws://192.168.0.101:1234", instead of " http://192.168.0.101 :1234" Bisakah Anda mencoba "ws://192.168.0.101:1234", alih-alih " http://192.168.0.101 :1234"

en

@Unforgiven-wanda contoh dengan React Native telah ditambahkan di sini: https://github.com/socketio/socket.io/tree/master/examples/react-native

en

Tidak dapat terhubung dengan "socket.io-client": "^3.0.4" dalam reaksi asli

en

@chiragpurohit71085 have you checked the example there: https://github.com/socketio/socket.io/tree/master/examples/react-native? @chiragpurohit71085 sudahkah Anda memeriksa contoh di sana: 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 ? Saya pikir ini adalah masalah URL, bisakah Anda memeriksa apakah server Anda dapat dijangkau di http://192.168.x.y:z ?

en
Apakah halaman ini membantu?
0 / 5 - 0 peringkat