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์ธ์ง ํ์ธํ๋ ๊ฒ์ด ์ข์ต๋๋ค. ์ฌ๊ธฐ์๋ ์น ์์ผ์ ๋ํ React Native ์์ ์ฌํญ์ด ํฌํจ๋์ด ์๊ธฐ ๋๋ฌธ์
๋๋ค. ๐
@benhjames @darrachequesne ์์ผ์ ์ฌ์ฉํ์ต๋๋ค. [email protected] ([email protected]๋ฅผ ๊ฐ์ ธ์ด) ๋ฐ node_modules์ ๋ณ๊ฒฝ ์ฌํญ์ ์ฌ์ ํ ํ์ธํ์ง๋ง ์๋ํ์ง ์์ต๋๋ค.
`'socket.io-client'์์ io ๊ฐ์ ธ์ค๊ธฐ;
๋ด๋ณด๋ด๊ธฐ ๊ธฐ๋ณธ ํด๋์ค HomeScreen์ React.Component {๋ฅผ ํ์ฅํฉ๋๋ค.
์์ฑ์(์ํ){
์ํผ(์ํ);
this.socket = io('ws://192.168.0.107:8080/endpoint',{
์ฌ์ฐ๊ฒฐ: ์ฌ์ค,
์ฌ์ฐ๊ฒฐ ์ง์ฐ: 500,
jsonp: ๊ฑฐ์ง,
์ฌ์ฐ๊ฒฐ ์๋ ํ์: ๋ฌดํ๋,
์ ์ก: ['์น ์์ผ']
});
this.socket.on('connect_error', (์ค๋ฅ) => {
console.log(์ค๋ฅ)
});
}
`
์๋์ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
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
์๋ ํฌ์ธํธ์ ๊ฐ์ ๋์๊ฒ ์ธ๋ชจ๊ฐ ์๋ชป๋์๋ค๊ณ - ๊ทธ๊ฒ์ํด์ผ http
๋๋ https
ํ๋กํ ์ฝ ๋งํฌ๊ฐ ์๋ ws
์ค ํ๋์
๋๋ค. ๐
@benhjames ์ด์ ์ WebSocket API 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 ํ์ด๋ก๋๋ฅผ ์ง๋ ฌํ ํด์ ํ๋ ์ฌ์ฉ์ ์ง์ ๋ ผ๋ฆฌ ์์ด WebSocket API์ ํจ๊ป ์๋ํ๋ ๊ฒฝ์ฐ ์๋ฒ๊ฐ socket.io๋ฅผ ์ฌ์ฉํ์ง ์๋๋ค๋ ์๋ฏธ์ ๋๋ค.
์ด๊ฒ์ด ์ฌ์ค์ธ์ง ํ์ธํ๊ธฐ ์ํด ์๋ฒ์์ ์ผ๋ถ ์ฝ๋๋ฅผ ์ ๊ณตํ ์ ์๋ ๊ธฐํ๊ฐ ์์ต๋๊น?
@benhjames ๋ง์ต๋๋ค . ๋ฐฑ์๋ ์ฝ๋์ https://docs.oracle.com/javaee/7/api/javax/websocket/package-summary.html ์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค.
๊ทธ๋ฌ๋ฉด ์ค๋ช ์ด ๋ฉ๋๋ค! Socket.io๊ฐ ์๋ํ๋ ค๋ฉด ํด๋ผ์ด์ธํธ์ ์๋ฒ _๋ชจ๋_์์ ์ฌ์ฉํด์ผ ํฉ๋๋ค. ๐
@benhjames @darrachequesne ์ด์ ๋ฐฑ์๋์์ socket.io๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. socket.io๋ฅผ ์ฌ์ฉํ๋ ๋ฐ๋๋ผ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ํ๋ก ํธ์๋์์ ์ ๋๋ก ์๋ํ์ง๋ง React Native๋ฅผ ํตํด socket.io๋ฅผ ์ฐ๊ฒฐํ๋ ค๊ณ ํ๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
๋ฐ๋๋ผ JS(Working Perfectly fine) :
```
<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๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์๋ฒฝํ๊ฒ ์๋ํ์ง๋ง ๋ฐ์ ๋ค์ดํฐ๋ธ์ ํจ๊ป ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ฑ๊ณต์ ์ธ ์ฐ๊ฒฐ์ ๋ณด์ฌ์ฃผ๋ ์๋ฒ๊ฐ ์์ง๋ง ๋ฐ์ ๋ค์ดํฐ๋ธ, ์๋์ ๊ฐ์ด ์ฐ๊ฒฐ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
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
...
์ถ์ :
NodeJS๋ฅผ ์ฌ์ฉํ๋ฉด ์ค๋ฅ ์์ด ์๋ฒฝํ๊ฒ ์๋ํฉ๋๋ค.
์ ๋ต๋ณ์ ์ฐธ๊ณ ํด์ฃผ์ธ์!
์ง์ํด ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. https://github.com/mrniko/netty-socketio/issues/614
์น ์์ผ์ด๋ ํด๋ง์ ์ ํํ๋๋ผ๋ ๋์ผํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. websocket_error ๋๋ xhr ํด๋ง ์ค๋ฅ์ ๋๋ค.
URL์ IP(http://192.168.1.112:3000)์ด๊ณ React Native Expo๋ฅผ ์ฌ์ฉํ์ฌ ์ค์ ์ ํ์์ ๋์ผํ ๋ก์ปฌ ๋คํธ์ํฌ์ ์ปดํจํฐ๋ก ์ฐ๊ฒฐํ๊ณ ์์ต๋๋ค.
์คํ ์ถ์ ์ด๋ ์ ๋ณด๊ฐ ๋ ํ์ํ๋ฉด ๋์๋๋ฆด ์ ์์ต๋๋ค...
socket.io 2.2.0 // ํด๋ผ์ด์ธํธ์ ์๋ฒ ๋ชจ๋
engine.io 3.3.2 // ํด๋ผ์ด์ธํธ์ ์๋ฒ ๋ชจ๋
์น ์์ผ๊ณผ ํด๋ง ์ ์ก์ด ๋ชจ๋ ์๋ํ์ง ์๋ ๊ฒฝ์ฐ socket.io ์ธ๋ถ์ ๋ฌธ์ ์ฒ๋ผ ๋ค๋ฆฝ๋๋ค. ๋ค์์ ์๋ํ ์ ์์ต๋๋ค.
fetch
์์ฒญ์ ํฉ๋๋ค. ๊ทธ๋๋ ์๋ํ์ง ์์ผ๋ฉด ๋คํธ์ํฌ ๋๋ React Native ํ๊ฒฝ์ ๋ฌธ์ ๊ฐ ์์ ์ ์์ต๋๋ค.์ฒซ ๋ฒ์งธ ํ ์คํธ๋ฅผ ์๋ํฉ๋๋ค. ์ ์์ ์ธ ๊ฐ์ ธ์ค๊ธฐ๊ฐ ์๋ํฉ๋๋ค.
ํธ์ง : 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์์ ๋ง์ง๋ง ๋ฒ์ ์ ์๋ํ์ฌ RN android ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ต๋๋ค.
npm i socketio/engine.io-client#3.3.2 -S
๋ฐ์ ๋ค์ดํฐ๋ธ 0.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?
๋ฏธ๋ฆฌ ๊ฐ์ฌํฉ๋๋ค!
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋ฐ์ ๋ค์ดํฐ๋ธ 0.60์์ ์๋ํ๋ ๊ฒ ๊ฐ์ง ์์ต๋๋ค.