Socket.io-client: لا يمكن الاتصال عبر socket.io-client على رد فعل أصلي

تم إنشاؤها على ٣١ أكتوبر ٢٠١٨  ·  37تعليقات  ·  مصدر: socketio/socket.io-client

You want to: اتريد:

  • [x] report a bug [x] أبلغ عن خطأ
  • [ ] request a feature [] طلب ميزة

Current behaviour السلوك الحالي

I can't connect via socket io to my server, it times out. لا يمكنني الاتصال بخادمي عبر المقبس io ، لقد انقضت المهلة.

Steps to reproduce (if the current behaviour is a bug) خطوات إعادة الإنتاج (إذا كان السلوك الحالي خطأ)

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: تبدو سجلات تصحيح الأخطاء كما يلي:

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 . ملحوظة : أفضل طريقة (ونعني بذلك الطريقة الوحيدة ) للحصول على إجابة سريعة هي تقديم حالة اختبار فاشلة عن طريق تزييف الكمان التالي.

Expected behaviour سلوك متوقع

I expect to be able to connect to my socketio server, the same way I am able to in a web browser. أتوقع أن أكون قادرًا على الاتصال بخادم socketio الخاص بي ، بنفس الطريقة التي يمكنني الاتصال بها في متصفح الويب.

Setup نصب

  • OS: iOS iPhone 10 simulator نظام التشغيل: iOS iPhone 10 simulator
  • browser: React-native المتصفح: تفاعل أصلي
  • socket.io version: 2.1.1 الإصدار socket.io: 2.1.1
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. تم العثور أخيرًا على حل لـ React Native> = 0.60 بعد بضع فناجين من القهوة والحفر بعمق في engine.io - بالإضافة إلى تعارض الإصدار (مع RN) يبدو أن هناك مشكلة في تمرير URI. When I pass an URI into socketIO, it gets turncated to the host and 'socket.io' appened. عندما أقوم بتمرير URI إلى socketIO ، يتم تحويله إلى المضيف ويتم تطبيق 'socket.io'. 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") سيؤدي إلى محاولة socketIO الاتصال بـ https://foo.bar/socket.io دون إظهاره في السجلات عند إلقاء خطأ سحب / مأخذ xhr.

Here we go, how to get socket.io running on current versions of React Native : هنا نبدأ ، كيفية تشغيل socket.io على الإصدارات الحالية من React Native :

  • use socket.io-client version _2.1.1_ استخدم الإصدار socket.io-client _2.1.1_
// package.json
{
  "dependencies": {
     "socket.io-client": "2.1.1"
   }
}
  • when setting up the client, define a host and a path عند إعداد العميل ، حدد مضيفًا ومسارًا
const socket = socketIO('https://your.host.com/', {
  path: '/path/to/socket.io/'
})
  • and suddenly everything works as smooth as planed وفجأة كل شيء يعمل بسلاسة كما تم التخطيط له

Hope to have saved you the time I spent on this issue 😆 آمل أن أكون قد وفرت لك الوقت الذي قضيته في هذه القضية

en

ال 37 كومينتر

Looks like this is react-native's issue. يبدو أن هذه مشكلة متعلقة برد الفعل. Earlier versions of RN seem to work with socket.io, but 0.0.57 does not. يبدو أن الإصدارات السابقة من RN تعمل مع socket.io ، لكن 0.0.57 لا تعمل. Closing. إغلاق.

en

Try "socket.io-client": "2.0.4" جرب "socket.io-client": "2.0.4"
This may be socket.io bug قد يكون هذا خطأ socket.io

en

Try "socket.io-client": "2.0.4" جرب "socket.io-client": "2.0.4"
This may be socket.io bug قد يكون هذا خطأ socket.io

Yes it does work. نعم إنه يعمل.

en

مع RN 0.52 و socket.io-client 2.0.4 ما زلت أواجه هذه المشكلة.

en

@noahprince22 can you please share your configuration ? @ noahprince22 هل يمكنك من فضلك مشاركة التكوين الخاص بك؟
I am currently using this and the issue still persist. أنا أستخدم هذا حاليًا وما زالت المشكلة قائمة.

"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? أي حل؟

socket.io team please, this should work with RN, is a socket.io bug. فريق socket.io من فضلك ، هذا يجب أن يعمل مع RN ، هو خلل في socket.io.

en

Hi @ningacoding , I managed to get it working with this configuration مرحبًا ningacoding ، لقد تمكنت من تشغيله مع هذا التكوين

"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 في تطبيقك الأصلي المتفاعل ، استخدم هذا

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

"react-native": "0.58.6", "تفاعل أصلي": "0.58.6"،
"react": "16.6.3", "رد فعل": "16.6.3"،
"socket.io-client": "^2.0.4", "socket.io-client": "^ 2.0.4"،

Not working for me لا يعمل لدي

en

any solution? اي حل؟ I'm facing the same thing أنا أواجه نفس الشيء

en

I have long since stopped working on the project that I was having this issue with. لقد توقفت منذ فترة طويلة عن العمل في المشروع الذي كنت أواجه هذه المشكلة معه. If I remember correctly, I downgraded react-native to "solve' the problem. إذا كنت أتذكر بشكل صحيح ، فقد قمت بخفض مستوى رد الفعل الأصلي من أجل "حل" المشكلة.

en

Experiencing the same issue, has there been a fix yet? تواجه نفس المشكلة ، هل كان هناك حل حتى الآن؟ Anyone find any workarounds to implementing sockets in React Native? هل وجد أي شخص أي حلول بديلة لتنفيذ المقابس في React Native؟

en

@russellgoldman I had problems with RN 0.60.5 and socket.io. russellgoldman لدي مشاكل مع RN 0.60.5 و socket.io. Release build did not connect to the server. لم يتصل بناء الإصدار بالخادم.

None of socket.io versions worked with RN 0.60.5: I tried versions 2.0.4, 2.1.1, 2.2.0. لم تعمل أي من إصدارات socket.io مع RN 0.60.5: لقد جربت الإصدارات 2.0.4 ، 2.1.1 ، 2.2.0.

Downgrading to RN 0.59.10 solved my problem. أدى الرجوع إلى RN 0.59.10 إلى حل مشكلتي.

en

Hi @anttirauh and @russellgoldman , مرحبًا anttirauh و @ russellgoldman ،
I tried with RN 0.59.10 and Socket 2.0.4, but its not connecting on release build. حاولت باستخدام RN 0.59.10 و Socket 2.0.4 ، لكن لم يتم الاتصال به عند إصدار الإصدار.
Can you please help me? هل تستطيع مساعدتي رجاء؟

en

@ Ramesh21071993 أعتقد أنه يجب عليك ترقية socket.io إلى الإصدار 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 ، لقد جربت جميع هذه الإصدارات socket io (2.0.4 ، 2.1.1 ، 2.2.0) والإصدارات الأصلية المتفاعلة (0.60 ، 0.59.10 ، 0.59.4)
In server side also socket io 2.0.4. في جانب الخادم أيضًا مقبس io 2.0.4.
still that same issue sir, لا تزال نفس المشكلة سيدي ،

But it works on RN 0.55.4 لكنها تعمل على RN 0.55.4

en

@Ramesh21071993 My understanding (which is formed with heavy googling and own testing) is that: @ Ramesh21071993 ما أفهمه (الذي يتكون من كثرة البحث على Google والاختبارات الخاصة) هو:

  • socket.io stopped working with react-native about around RN version 0.57.4 socket.io توقف عن العمل مع التفاعل الأصلي حول إصدار RN 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 أو بعض المكونات الأخرى لاحقًا وتمكنت من تشغيله مع الإصدار combo: RN 0.59.10 + socket.io 2.1.1
  • RN version 0.60.x is not working with any version of socket.io RN الإصدار 0.60.x لا يعمل مع أي إصدار من 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. لذا نصيحتي هي فقط: حاول مرة أخرى باستخدام RN 0.59.10 و socket.io 2.1.1 وتأكد من مسح مجلد الإنشاء قبل الإنشاء. And remove node_modules before yarn/npm. وإزالة node_modules قبل الغزل / npm.

en

@anttirauh Sir, below is my package.json. anttirauh سيدي ، أدناه حزامي. json. I did as what you told. فعلت ما قلته. But nothing happen. لكن لم يحدث شيء. I remove build folder and node modules and again i install. أقوم بإزالة مجلد البناء ووحدات العقد ثم أقوم بالتثبيت مرة أخرى.
Please help me on this to work, Past one month am trying الرجاء مساعدتي في هذا العمل ، الشهر الماضي وأنا أحاول

App package.json حزمة التطبيق json

{ {
"name": "Track", "الاسم": "المسار"،
"version": "0.0.1", "الإصدار": "0.0.1"،
"private": true, "خاص": صحيح ،
"scripts": { "نصوص": {
"start": "node node_modules/react-native/local-cli/cli.js start", "البدء": "node node_modules / reaction-native / local-cli / cli.js start"،
"test": "jest" "test": "jest"
}, } ،
"dependencies": { "التبعيات": {
"react": "16.8.3", "رد فعل": "16.8.3"،
"react-native": "0.59.10", "تفاعل أصلي": "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-jest": "^ 24.9.0"،
"jest": "^24.9.0", "jest": "^ 24.9.0"،
"metro-react-native-babel-preset": "^0.56.0", "metro-reaction-native-babel-preset": "^ 0.56.0"،
"react-test-renderer": "16.8.3" "رد فعل-اختبار-عارض": "16.8.3"
}, } ،
"jest": { "دعابة": {
"preset": "react-native" "ضبط مسبق": "تفاعل أصلي"
} }
} }

Server Package.json حزمة الخادم. json

{ {
"name": "TRACK-Socket", "الاسم": "TRACK-Socket"،
"version": "0.0.1", "الإصدار": "0.0.1"،
"dependencies": { "التبعيات": {
"amqplib": "^0.5.1", "amqplib": "^ 0.5.1"،
"config": "^3.2.2", "التكوين": "^ 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 شكرًا

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. تم العثور أخيرًا على حل لـ React Native> = 0.60 بعد بضع فناجين من القهوة والحفر بعمق في engine.io - بالإضافة إلى تعارض الإصدار (مع RN) يبدو أن هناك مشكلة في تمرير URI. When I pass an URI into socketIO, it gets turncated to the host and 'socket.io' appened. عندما أقوم بتمرير URI إلى socketIO ، يتم تحويله إلى المضيف ويتم تطبيق 'socket.io'. 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") سيؤدي إلى محاولة socketIO الاتصال بـ https://foo.bar/socket.io دون إظهاره في السجلات عند إلقاء خطأ سحب / مأخذ xhr.

Here we go, how to get socket.io running on current versions of React Native : هنا نبدأ ، كيفية تشغيل socket.io على الإصدارات الحالية من React Native :

  • use socket.io-client version _2.1.1_ استخدم الإصدار socket.io-client _2.1.1_
// package.json
{
  "dependencies": {
     "socket.io-client": "2.1.1"
   }
}
  • when setting up the client, define a host and a path عند إعداد العميل ، حدد مضيفًا ومسارًا
const socket = socketIO('https://your.host.com/', {
  path: '/path/to/socket.io/'
})
  • and suddenly everything works as smooth as planed وفجأة كل شيء يعمل بسلاسة كما تم التخطيط له

Hope to have saved you the time I spent on this issue 😆 آمل أن أكون قد وفرت لك الوقت الذي قضيته في هذه القضية

en

@rastapasta , i got an error like “ CLEARTEXT communication not permitted by network security policy ”, rastapasta ، تلقيت خطأ مثل " اتصال CLEARTEXT غير مسموح به بواسطة سياسة أمان الشبكة " ،

Please tell me what to do ارجوك قل لي ما العمل

en

@Ramesh21071993 if you can't use your endpoint via https (please consider it), add their hostnames to the exception lists. @ Ramesh21071993 إذا لم تتمكن من استخدام نقطة النهاية الخاصة بك عبر https (يرجى النظر في الأمر) ، أضف أسماء المضيف الخاصة بهم إلى قوائم الاستثناءات.

for iOS: https://stackoverflow.com/a/30732693 لنظام iOS: https://stackoverflow.com/a/30732693
for Android: https://stackoverflow.com/a/50834600 لنظام Android: https://stackoverflow.com/a/50834600

en

Sir, I tried that also but not able to do. سيدي ، لقد حاولت ذلك أيضًا ولكني لم أستطع فعل ذلك. If you don't mind can you please give me any demo source files إذا كنت لا تمانع ، يمكنك أن تعطيني أي ملفات مصدر تجريبي
For long time I struck with this. لفترة طويلة كنت مع هذا.

Am using ip and port sir أنا باستخدام الملكية الفكرية والميناء سيدي

en

اتبع توجيهات الوثائق المذكورة أعلاه وقم بتكييف هذا التنفيذ:

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

سيدي ، في جانب الخادم أنا أستخدم المقبس io 2.1.1 ، هل هو جيد أو يحتاج إلى تغيير أي شيء

en

image

en

Lizhooh طريقة الاختراق لاكتشاف whazzup على rn: monkey patch a console.log(err) قبل self.onError في node_modules engine.io-client / blob / master / lib / transports / الاقتراع- xhr.js # L131

en

@Ramesh21071993 Is your issue resolved? @ Ramesh21071993 هل تم حل مشكلتك؟ I am facing the same issue. أواجه نفس المشكلة. Tried everything mentioned in above comments. حاولت كل ما هو مذكور في التعليقات أعلاه.

en

@rastapasta I followed what you have suggested above. rastapasta لقد تابعت ما اقترحته أعلاه. My socket connection happens and then immediately disconnects. يحدث اتصال المقبس الخاص بي ثم يتم قطع الاتصال على الفور. I get timeout error infinitely. أحصل على خطأ مهلة لانهائية. However, whenever I click on some component which makes a network call, the socket connects and immediately disconnects. ومع ذلك ، كلما نقرت على بعض المكونات التي تجري مكالمة شبكة ، يتصل المقبس ويفصل على الفور. If I don't do any network call after first socket disconnect, it would stay in the same state. إذا لم أقم بأي مكالمة شبكة بعد قطع الاتصال بالمقبس الأول ، فسيظل في نفس الحالة. Please, could you help? من فضلك ، هل يمكنك المساعدة؟

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' ، // لقد أعطيت هذا هو المسار في خادم المقبس الخاص بي بالإضافة إلى ملف إدخال kubernetes الخاص بي للتوجيه إلى هذا المنفذ
transports: ['websocket'], النقل: ['websocket'] ،
reconnectionDelay: 1000, التأخير: 1000 ،
reconnection: true, إعادة الاتصال: صحيح ،
reconnectionAttempts: Infinity, إعادة الاتصال المحاولات: إنفينيتي ،
// timeout: 30000, // المهلة: 30000 ،
jsonp: false jsonp: خطأ
}); }) ؛

things that I have tried: الأشياء التي جربتها:

  1. removing and adding transports option إزالة وإضافة خيار النقل
    2.removing and add timeout 2. إزالة وإضافة مهلة
  2. removing all parameters except path إزالة كافة المعلمات باستثناء المسار
  3. adding pingTimeout and pingInterval in server socket configuration إضافة pingTimeout و pingInterval في تكوين مقبس الخادم
  4. removing path from server and client and only keeping it in my kubernetes ingress file to route to that deployment/pod. إزالة المسار من الخادم والعميل والاحتفاظ به فقط في ملف إدخال kubernetes الخاص بي للتوجيه إلى هذا النشر / pod.

Note: the same server and kubernetes conf work for any node.js socket client as well as my react js web app. ملاحظة: يعمل نفس الخادم و kubernetes conf مع أي عميل مقبس node.js بالإضافة إلى تطبيق الويب الخاص بتفاعل js. I am only facing this issue for React Native app in debug as well as release mode. أواجه هذه المشكلة فقط لتطبيق React Native في وضع التصحيح بالإضافة إلى وضع الإصدار.

en

هل لدى أي شخص أي فكرة عن مشكلتي أعلاه؟

en

@calqlum123 يارب احفظها
Please make sure to يرجى التأكد من

  • add your host to the transport exception list (s. here ) أضف مضيفك إلى قائمة (قوائم استثناء النقل هنا )
  • try it without manually defining the transports جربه بدون تحديد وسائل النقل يدويًا
  • monkey patch the console.log (s. here ) and check the error قرد تصحيح ملف console.log (s. هنا ) وتحقق من الخطأ
en

Can anyone please share a complete working example for both client and server? هل يمكن لأي شخص أن يشارك في مثال عملي كامل لكل من العميل والخادم؟ Because this is confusing. لأن هذا محير.

en

I am using "react-native": "0.61.5" and "socket.io-client": "^2.1.1", أنا أستخدم "رد فعل أصلي": "0.61.5" و "socket.io-client": "^ 2.1.1" ،

below is my 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. الرمز غير قادر على الاتصال بالخادم وأنا أيضًا أبحث عن الحل. I don't want to downgrade my react-native version. لا أريد الرجوع إلى إصدار أقدم من إصدار رد الفعل الأصلي الخاص بي.

en

you must add this code in application tag يجب عليك إضافة هذا الرمز في علامة التطبيق
like below in AndroidManifest.xml كما هو موضح أدناه في AndroidManifest.xml
. . . . . .

en

I am using "react-native": "0.61.5" and "socket.io-client": "^2.1.1", أنا أستخدم "رد فعل أصلي": "0.61.5" و "socket.io-client": "^ 2.1.1" ،

below is my 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. الرمز غير قادر على الاتصال بالخادم وأنا أيضًا أبحث عن الحل. I don't want to downgrade my react-native version. لا أريد الرجوع إلى إصدار أقدم من إصدار رد الفعل الأصلي الخاص بي.

Can you try "ws://192.168.0.101:1234", instead of " http://192.168.0.101 :1234" هل يمكنك تجربة "ws: //192.168.0.101: 1234" ، بدلاً من " http://192.168.0.101 : 1234"

en

@ Unforgiven-wanda تمت إضافة مثال مع React Native هنا: https://github.com/socketio/socket.io/tree/master/examples/react-native

en

غير قادر على الاتصال بـ "socket.io-client": "^ 3.0.4" في رد فعل أصلي

en

@chiragpurohit71085 have you checked the example there: https://github.com/socketio/socket.io/tree/master/examples/react-native? @ chiragpurohit71085 هل راجعت المثال هناك: 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 ? أعتقد أن هذه مشكلة في عنوان URL ، هل يمكنك التحقق مما إذا كان يمكن الوصول إلى الخادم الخاص بك على http://192.168.x.y:z ؟

en
هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات