2.1.1に戻すと、問題が修正されます。
新しいcors管理の変更と関係があるのではないかと思います。
原点を手動で設定しても何も起こらないようです。
反応ネイティブデバッガーを起動すると、魔法のように動作を開始します。
reconnection: true,
reconnectionDelay: 500,
jsonp: false,
reconnectionAttempts: Infinity,
transports: ['websocket']
両方のプラットフォーム(シミュレーター)2.1.1で同じ問題が発生しましたが、デバッガーをオンにした場合にのみ機能します。
私もこの問題に直面しています。 最初の接続遅延の問題が修正されるかどうかを確認するためにアップグレードしました。
私も同じように直面しています。 問題
2.1.1に戻すことは私にとってはうまくいきます。
私はまだ同じ問題に直面しており、2.1.1を元に戻すこともできません。
https://github.com/socketio/engine.io-client/pull/607で修正する必要があり
@benhjames修正により、React Nativeの問題が修正されたことを確認できますか?
はい、私は現在React Nativeで問題なく[email protected]
( [email protected]
を取得しています)を実行しています。
engine.io-client
が3.3.2であることを確認する価値があります。これには、WebSocketのReactNative修正が含まれているためです。 😃
@ benhjames @ darrachequesne私はソケットを使用しました。 [email protected] (
`import io from'socket.io-client ';
デフォルトクラスのエクスポートHomeScreenはReact.Componentを拡張します{
コンストラクター(小道具){
スーパー(小道具);
this.socket = io( 'ws://192.168.0.107:8080 / endpoint'、{
再接続:true、
reconnectionDelay:500、
jsonp:false、
reconnectionAttempts:Infinity、
トランスポート:['websocket']
});
this.socket.on( 'connect_error'、(err)=> {
console.log(err)
});
}
`
以下のようなエラーが発生します:
Error: websocket error
at WS.Transport.onError (transport.js:67)
at WebSocket.ws.onerror (websocket.js:157)
at WebSocket.dispatchEvent (event-target.js:172)
at WebSocket.js:289
at RCTDeviceEventEmitter.emit (EventEmitter.js:190)
at MessageQueue.__callFunction (MessageQueue.js:349)
at MessageQueue.js:106
at MessageQueue.__guard (MessageQueue.js:297)
at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:105)
at debuggerWorker.js:72
エンドポイントが正しくないように見えます。 ws
ではなく、 http
またはhttps
プロトコルリンクである必要があります。 👍
@benhjames以前にWebSocketAPI https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_APIで試しましたが、同じプロトコルで完全に正常に機能していました。
socket.io(WebSocket APIとは異なり)は、クライアントのHTTP(S)プロトコルURIのみを受け入れます(ここのドキュメントを参照)が、バックグラウンドでWSプロトコルを使用します。
したがって、 io('http://192.168.0.107:8080/endpoint', ...)
すると、機能するはずです。
@benhjamesアップデートしてくれてありがとう!
ws://
は正しく機能するはずですが、 /endpoint
は、エンドポイント名前空間に接続することを意味します(v2.x以降の現在の実装です)。 あなたは使用したいかもしれません:
this.socket = io('ws://192.168.0.107:8080', {
path: '/endpoint',
reconnection: true,
reconnectionDelay: 500,
jsonp: false,
reconnectionAttempts: Infinity,
transports: ['websocket']
});
@darrachequesne上記の実装を試しましたが、まだ機能していません。
おそらく、SocketIOサーバーコードがどのように見えるかを確認して、適切な名前空間や設定などを使用していることを確認すると役立つ場合があります。🙂
WebSocket APIで完全に正常に動作していますが、Socket.io APIに接続しようとすると、次のようなエラーが発生します。
websocket error
Stack trace:
null:null in WS.Transport.onError
node_modules/engine.io-client/lib/transports/websocket.js:178:8 in WebSocket.ws.onerror
node_modules/event-target-shim/lib/event-target.js:172:38 in WebSocket.dispatchEvent
node_modules/react-native/Libraries/WebSocket/WebSocket.js:289:13 in <unknown>
node_modules/react-native/Libraries/vendor/emitter/EventEmitter.js:190:32 in RCTDeviceEventEmitter.emit
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:349:41 in MessageQueue.__callFunction
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:106:11 in <unknown>
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:297:8 in MessageQueue.__guard
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:105:9 in MessageQueue.callFunctionReturnFlushedQueue
http://192.168.0.103:19001/debugger-ui/debuggerWorker.js:72:58 in <unknown>
socket.ioペイロードを逆シリアル化するカスタムロジックなしでWebSocketAPIと連携する場合、サーバーがsocket.ioを使用していないことを意味します。
これが当てはまるかどうかを確認するために、サーバーからコードを提供できる可能性はありますか?
@benhjamesそうです、バックエンドコードにhttps://docs.oracle.com/javaee/7/api/javax/websocket/package-summary.htmlを使用しています。
それはそれを説明します! Socket.ioを機能させるには、クライアントとサーバーの両方で使用する必要があります。 👍
@benhjames @darrachequesne現在、バックエンドでsocket.ioを使用していますが、socket.ioを使用してバニラJavaScriptを使用してフロントエンドで正常に動作していますが、React Nativeを介してsocket.ioに接続しようとすると、エラーが発生します。
バニラJS(完全に正常に動作):
`` `
<script>
var socket = io.connect('http://localhost:9092');
socket.on('connect', function() {
output('<span class="connect-msg">Client has connected to the server!</span>');
});
**React Native code: (Not working)**
this.socket = io.connect('http://localhost:9092',{
reconnection: true,
reconnectionDelay: 500,
jsonp: false,
reconnectionAttempts: Infinity,
transports: ['websocket']});
this.socket.on('connect',function(e){
console.log("on Connect");
})
this.socket.on('connect_error', (err) => {
console.log(err)
});
**Error:**
17:15
websocket error
Stack trace:
node_modules/engine.io-client/lib/transport.js:67:22 in onError
node_modules/engine.io-client/lib/transports/websocket.js:157:17 in onerror
node_modules/event-target-shim/lib/event-target.js:172:43 in dispatchEvent
node_modules/react-native/Libraries/WebSocket/WebSocket.js:290:10 in
node_modules/react-native/Libraries/vendor/emitter/EventEmitter.js:191:12 in emit
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:349:47 in __callFunction
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:106:26 in
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:297:10 in __guard
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:105:17 in callFunctionReturnFlushedQueue
```
I am using https://github.com/mrniko/netty-socketio in backend.
React Native内のローカルホスト以外のURL(つまりコンピューターのIP)に接続する必要がある場合があります。使用しているシミュレーターによっては、確かにAndroidで、場合によってはiOSで。
それでも機能しない場合は、 transports: ['polling']
オプションを試して、特にWebSocketトランスポートが機能していないのか、それともより一般的なネットワーク接続の問題なのかを確認する価値があります。
@benhjames私は奇妙な問題に直面しています。 私はバックエンドでhttps://github.com/mrniko/netty-socketioを使用しており、上記のようにVanilla JSを使用している場合は完全に正常に機能していますが、react-nativeで使用している場合、サーバーは正常な接続を示していますが、 react-native、以下のような接続エラーが発生しています:
server error
Stack trace:
node_modules/engine.io-client/lib/socket.js:453:28 in onPacket
node_modules/engine.io-client/lib/socket.js:276:18 in <unknown>
node_modules/component-emitter/index.js:133:25 in emit
node_modules/engine.io-client/lib/transport.js:148:12 in onPacket
node_modules/engine.io-client/lib/transports/polling.js:144:18 in callback
node_modules/engine.io-parser/lib/browser.js:384:20 in decodePayload
node_modules/engine.io-client/lib/transports/polling.js:148:23 in onData
node_modules/engine.io-client/lib/transports/polling-xhr.js:127:16 in <unknown>
node_modules/component-emitter/index.js:133:25 in emit
node_modules/engine.io-client/lib/transports/polling-xhr.js:300:12 in onData
node_modules/engine.io-client/lib/transports/polling-xhr.js:367:16 in onLoad
node_modules/engine.io-client/lib/transports/polling-xhr.js:253:10 in onreadystatechange
node_modules/event-target-shim/lib/event-target.js:172:43 in dispatchEvent
node_modules/react-native/Libraries/Network/XMLHttpRequest.js:570:23 in setReadyState
node_modules/react-native/Libraries/Network/XMLHttpRequest.js:392:25 in __didCompleteResponse
node_modules/react-native/Libraries/vendor/emitter/EventEmitter.js:191:12 in emit
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:349:47 in __callFunction
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:106:26 in <unknown>
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:297:10 in __guard
node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:105:17 in callFunctionReturnFlushedQueue
...
PS:
NodeJSを使用すると、エラーなしで完全に正常に動作します。
上記の回答をご覧ください!
ありがとう、 @ benhjames 、あなたのサポートのために、それは今働いています。 https://github.com/mrniko/netty-socketio/issues/614
WebSocketを選択した場合でも、ポーリングを選択した場合でも、同じ問題が発生します。 websocket_errorまたはxhrポーリングエラーのいずれかです。
URLはIP(http://192.168.1.112:3000)であり、React NativeExpoを使用して物理的な電話から同じローカルネットワーク上のコンピューターに接続しています。
より多くのスタックトレースまたは情報が必要な場合は、私がお手伝いします...
socket.io 2.2.0 //クライアントとサーバーの両方
engine.io 3.3.2 //クライアントとサーバーの両方
WebSocketとポーリングトランスポートの両方が機能していない場合は、socket.ioの外部で問題が発生しているように聞こえます。 あなたは試してみたいかもしれません:
fetch
リクエストを送信します。 それが機能しない場合は、ネットワークまたはReactNative環境に問題がある可能性があります。最初のテストを試してみると、通常のフェッチが機能します
編集:CORS関連の問題のようです。 単純なJSクライアントに接続しようとすると、次のようになります。
Access to XMLHttpRequest at 'http://192.168.1.112:3000/socket.io/?EIO=3&transport=polling&t=MfV8vhI' from origin 'http://192.168.1.112:5000' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
これを設定する方法について何かアドバイスはありますか?
このように、githubの最後のバージョンを試して、RNandroidの問題を解決しました。
npm i socketio/engine.io-client#3.3.2 -S
何もreactnative0.60で動作しているようには見えません
RN 0.60.5を使用すると、Androidリリースビルドが機能しません
socket.io-clientバージョン2.0.4、2.1.1、2.2.0を試しました。
yarn add socketio/engine.io-client#3.3.2
試しました
#1305を参照してください-Androidでは、httpsを使用するか、AndroidManifestでusesCleartextTraffic属性を設定する必要があります
v2.3をお試しください
Expo SDK v34およびv35(RN 0.59)を試してみましたが、他のバージョン(2.1.1、2.1.0、2.2.0)と同様に、socket.io-clientを1.7.2( yarn add [email protected]
)にダウングレードする必要がありました。うまくいかない。 私は2.3.0を試しませんでしたが、動作するようになったので、問題は発生しません:-)
こんにちは@lionbur 、私が知っているように、0.59の多くのミニバージョンがあります。 どのバージョンを使用していますか? 0.59.9?
前もって感謝します!
最も参考になるコメント
何もreactnative0.60で動作しているようには見えません