react-native์์ ์น ์์ผ ์ฐ๊ฒฐ์ ์๋ํ๋ ๋ฐ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ์ด ํ๋ซํผ์ด ์ง์๋๋์ง ์ฌ๋ถ๋ ํ์คํ์ง ์์ง๋ง ์ด ์ค๋ฅ๋ ์ผ๋ฐ node.js ์ฑ์์ ์ฌํํ ์ ์์ต๋๋ค. ์ด์ํ๊ฒ๋ react-native์ ๋ด์ฅ ๋๋ฒ๊ฑฐ๊ฐ ๋นํ์ฑํ ๋ ๊ฒฝ์ฐ์๋ง ๋ฌธ์ ๊ฐ ์์ต๋๋ค. react-native v0.43.1
๋ฐ socket.io-client v1.7.3
ํ์ฌ IOS 10.2์์ ์ด๊ฒ์ ํ
์คํธํ๊ณ ์์ต๋๋ค. ๋ด ์๋ฒ๋ HTTP ํฌํธ 1337์์ ํธ์คํ
๋๋ socket.io v1.7.2
์คํ ์ค์
๋๋ค.
react-native์์ socket.io-client
๋ฅผ ๊ตฌํํ๊ณ ๋๋ฒ๊ฑฐ๋ฅผ ํ์ฑํ ํ์ง ์๊ณ ์ฐ๊ฒฐ XHR: POLLING ERROR
๋ฉ๋๋ค. ๋ด ์ ์ก ์ต์
์์ 'websocket'์ ์ง์ ํ๋ฉด ์ค๋ฅ๋ WEBSOCKET ERROR
๋ฉ๋๋ค. ๊ทธ๊ฒ ๋ด๊ฐ ์ป๋ ์ ๋ถ์ผ.
๋ด ์๋ฒ์ ๋ํ ์ผ๋ฐ ์น ์์ผ ์ฐ๊ฒฐ. ๋๋ฒ๊ฑฐ๊ฐ ํ์ฑํ๋ ์ํ์์ ์๋ํฉ๋๋ค.
์ด ๋ฌธ์ ์ ๊ด๋ จํ์ฌ ๋ด๊ฐ ์ฐพ์ ์ ์ผํ ๋ค๋ฅธ ๋ฌธ์ ๋ #1056์ด์๊ณ ์ด๋ฌํ ๋ฌธ์ ๊ฐ ์ผ๋ง๋ ์ด์ํ๊ฒ ๊ด๋ จ๋์ด ์๋์ง์ ๋ํด ๋๋์ต๋๋ค. ์ผ๋ฐ node.js ์ฑ์์ ์๋๋ฆฌ์ค๋ฅผ ๋ค์ ์๋ํ๊ณ ๋น์ทํ ๊ฒฐ๊ณผ๋ฅผ ๊ฒฝํํ์ง๋ง ์ค๋ฅ๋ ์๋์ ๊ฐ์ด ๋ ์์ธํ ์ค๋ช ๋์์ต๋๋ค.
์ง์ ๋ ์ ์ก ์ต์ ์์ด:
{ Error: xhr poll error
at XHR.Transport.onError (/path/to/root/node_modules/engine.io-client/lib/transport.js:64:13)
at Request.<anonymous> (/path/to/root/node_modules/engine.io-client/lib/transports/polling-xhr.js:129:10)
at Request.Emitter.emit (/path/to/root/node_modules/component-emitter/index.js:133:20)
at Request.onError (/path/to/root/node_modules/engine.io-client/lib/transports/polling-xhr.js:307:8)
at Timeout._onTimeout (/path/to/root/node_modules/engine.io-client/lib/transports/polling-xhr.js:254:18)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5) type: 'TransportError', description: 503 }
transports: ['websocket']
:
{ Error: websocket error
at WS.Transport.onError (/path/to/root/node_modules/engine.io-client/lib/transport.js:64:13)
at WebSocket.ws.onerror (/path/to/root/node_modules/engine.io-client/lib/transports/websocket.js:149:10)
at WebSocket.onError (/path/to/root/node_modules/ws/lib/WebSocket.js:452:14)
at emitOne (events.js:96:13)
at WebSocket.emit (events.js:189:7)
at ClientRequest.onerror (/path/to/root/node_modules/ws/lib/WebSocket.js:711:10)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:189:7)
at TLSSocket.socketErrorListener (_http_client.js:358:9)
at emitOne (events.js:96:13)
type: 'TransportError',
description:
{ Error: socket hang up
at TLSSocket.onHangUp (_tls_wrap.js:1117:19)
at Object.onceWrapper (events.js:291:19)
at emitNone (events.js:91:20)
at TLSSocket.emit (events.js:186:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
code: 'ECONNRESET',
type: 'error',
target:
WebSocket {
domain: null,
_events: [Object],
_eventsCount: 4,
_maxListeners: undefined,
_socket: null,
_ultron: null,
_closeReceived: false,
bytesReceived: 0,
readyState: 0,
supports: [Object],
extensions: {},
_binaryType: 'nodebuffer',
_isServer: false,
url: 'wss://SERVER_IP:1337/socket.io/?EIO=3&transport=websocket',
protocolVersion: 13 } } }
๋ํ socket.io์ ๋ธ๋ผ์ฐ์ ๊ธฐ๋ฐ ํด๋ผ์ด์ธํธ๋ ์ด ํน์ ์น ์๋ฒ์ ์๋ฒฝํ๊ฒ ์ฐ๊ฒฐํ์ฌ ์๋ํฉ๋๋ค.
๋ค์ํ ๋คํธ์ํฌ, ISP ๋ฐ VPN์ ์ฌ์ฉํด ๋ณด์์ต๋๋ค. ์ด ์์ ์์ ๋๋ ๋จ์๊ฐ ์์ต๋๋ค.
์ ๋ฐ์ดํธ : ๋ง์ง๋ง์ผ๋ก socket.io-client๋ฅผ ์ฌ์ฉํ์ฌ iPhone์์ ๋ด ์์ฉ ํ๋ก๊ทธ๋จ ์ค ํ๋๋ฅผ ํ ์คํธํ ํ ์ด์ด ์ข๊ฒ๋ XHR ํด๋ง ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ฌ์ ํ ์ผ๋ฐ ๋ ธ๋ ์ฑ์์ ์ด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
ํ์ฌ ๋
ธ๋ ์์ฉ ํ๋ก๊ทธ๋จ์๋ ๋์ผํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๋๋ socket.io์ ๋ด์ฅ ์๋ฒ๋ฅผ ์ฌ์ฉํ๊ณ ๋ธ๋ผ์ฐ์ ์์๋ ์ ์๋ํ๋ ๊ฒ ๊ฐ์ง๋ง ๋
ธ๋ ์ฑ socket.io-client์์๋ ์์ ๋์ผํ 503 xhr ํด๋ง ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. ๋์ผํ ์ปดํจํฐ์์ ์๋ฒ์ ํด๋ผ์ด์ธํธ๋ฅผ ์คํํ๊ณ ์์ง๋ง ํด๋ผ์ด์ธํธ ์์ฉ ํ๋ก๊ทธ๋จ์์ ๋ฃจํ๋ฐฑ ๋๋ ๋ก์ปฌ ๋คํธ์ํฌ ์ฃผ์์ ์ฐ๊ฒฐํ ์ ์์ต๋๋ค. socket.io
๋ฐ socket.io-client
1.7.3
๋ฒ์ ์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค.
๋
ธ๋์์ ์คํํ ๋ ๋ฒ์ 2.0.1
(ํด๋ผ์ด์ธํธ ๋ฐ ์๋ฒ)์์๋ ์ด ์ค๋ฅ๊ฐ ํ์๋ฉ๋๋ค.
์ผ์ฃผ์ผ ์ ์ ๋ฆฌํฉํ ๋งํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ง๋ง ์ ํํ ๋ฌด์์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋์ง ๊ธฐ์ต๋์ง ์์ต๋๋ค. ์ฃ์กํฉ๋๋ค. ๋๋ ๋ฐ์ ํ ์ฃผ๋ฅผ ๋ณด๋๊ณ ๊ทธ๊ฒ์ด ์ด๊ธฐ ์ปค๋ฐ์ด์์ต๋๋ค.
ํด๋ผ์ด์ธํธ์์ opts.transports
๋ฅผ websocket
๋ก๋ง ์ค์ ํ ์ ์์ต๋๋ค.
io.connect(url, { transports: ['websocket'] }); // default is ['polling', 'websocket']
์ฝ๋๋ฅผ ๋ฆฌํฉํ ๋งํ๊ธฐ ์ ์ ๋๋ฌด ๋ณต์กํ ์์ผ ๋ฐ ์์ ๋ฐ์ธ๋ฉ ๋ ผ๋ฆฌ๊ฐ ์์์ต๋๋ค. ๋ฐ๋ผ์ ์ฝ๋์ ์์ผ ๋ ผ๋ฆฌ๊ฐ ์๋ํ๋์ง ํ์ธํ๊ณ ๋๋ฒ๊ทธ๋ฅผ ๋ ๊ฐ๋จํ๊ฒ ๋ง๋์ญ์์ค.
์ด์กํด ๋ณด๊ฒ ์ต๋๋ค. ์ง๊ธ์ 1.7.4๋ก ๋ค์ด๊ทธ๋ ์ด๋ํ์ผ๋ฉฐ ๋ชจ๋ ์๋ํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
์, 2.0.1์์ 1.7.4๋ก ๋ฒ์ ์ ๋ฎ์ถ์์ต๋๋ค.
๊ทธ๋์ 2.0.1
๋ก ๋ค์ ์
๊ทธ๋ ์ด๋ํ๊ณ ์น ์์ผ ์ ์ก๋ง ์ฌ์ฉํ๋๋ก ํด๋ผ์ด์ธํธ๋ฅผ ์ค์ ํ์ต๋๋ค. ๊ทธ๊ฒ์ (๊ฑฐ์) ์๋ํฉ๋๋ค:
io(`localhost:8080`, {
transports: [ 'websocket' ],
upgrade: false
});
์ด์ ๋ด ์ ์ผํ ๋ฌธ์ ๋ ์น ์์
์ ๋ฒ์์ ๋ํด websocket ์ ์ก์ด ์ค๋จ๋์๋ค๋ ๊ฒ์
๋๋ค. ๋๋ ๋ฌธ์ ๋ฅผ ์๋ณํ๊ณ engine.io-client
์ ํด๋น ๋ฌธ์ ๋ฅผ ์ด์๋ค๊ณ ์๊ฐํฉ๋๋ค. https://github.com/socketio/engine.io-client/issues/569
์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๊น? ๋๋ฒ๊ทธ๋ฅผ ํ์ฑํํ ์ํ์์ ์คํํ ์ ์์ต๋๊น? ( DEBUG=engine*
)
react-native์ ๊ด๋ จํ์ฌ https://github.com/socketio/socket.io-client/issues/1114์ ๋งํฌ๋ ์ ์์ต๋๋ค
@darrachequesne ๊ทธ๋์ ๊ทธ๋ฅ ๋ด ์๋ฒ ์ธก ํด๋ผ์ด์ธํธ ๊ตฌ์ฑ ์์์์ ๊ธฐ๋ณธ ์ ์ก์ผ๋ก ๋๋๋ ธ์ต๋๋ค. ๋ด ์ค์๋ ... ํฌํ ์ ์ก์ ํ์คํ ๋๋ฅผ ์ํด ์ผํ๊ณ ์์ต๋๋ค. ๋ด๊ฐ ๊ฐ์ง ๋ฌธ์ ๋ ๋ก์ปฌ ๊ฐ๋ฐ์์ ๋ํ ์์ฒด ์๋ช
SSL ์ธ์ฆ์์ ๊ด๋ จ๋ ๊ฒ์
๋๋ค. ์๋ํ๋ ค๋ฉด socket.io ๊ตฌ์ฑ์ rejectUnauthorized: process.env.NODE_ENV === 'production'
๋ฅผ ์ถ๊ฐํด์ผ ํ์ต๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด 503
๋ฐ๊ณ ์์์ต๋๋ค.
๊ทธ๋ฌ๋ ์น ์์ ์ ๋ฌธ์ ๋ ํ์ค์ ๋๋ค. socketio/engine.io-client#569
์ด ์๋
require('socket.io-client')('http://localhost', {
rejectUnauthorized: false,
})
๊ฐ์ฌํฉ๋๋ค ์๋ํด๋ณด๊ฒ ์ต๋๋ค
------ ์๋ณธ ๋ฉ์์ง-----
๋ณด๋ธ ์ฌ๋: Zetsin [email protected]
๋ฐ๋ ์ฌ๋: "socketio/socket.io-client" [email protected]
์ฐธ์กฐ: Wangzhiqiang [email protected] , ๋๊ธ [email protected]
์ ๋ชฉ: Re: [socketio/socket.io-client] ๋๋ฒ๊ฑฐ๊ฐ ์์ ๋ XHR ํด๋ง ์ค๋ฅ๊ฐ ๋ํ๋จ(#1097)
๋ ์ง: 2017๋
5์ 20์ผ 16:22
์ด ์๋
require('socket.io-client')('http://localhost', {
๊ฑฐ๋ถ ๊ถํ ์์: ๊ฑฐ์ง,
})
โ
๋น์ ์ด ๋๊ธ์ ๋ฌ์๊ธฐ ๋๋ฌธ์ ์ด๊ฒ์ ๋ฐ๋ ๊ฒ์
๋๋ค.
์ด ์ด๋ฉ์ผ์ ์ง์ ๋ต์ฅํ๊ฑฐ๋ GitHub์์ ๋ณด๊ฑฐ๋ ์ค๋ ๋๋ฅผ ์์๊ฑฐํ์ธ์.
๊ฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๊ทธ๋ฐ ๋ค์ ํด๋ผ์ด์ธํธ ์ฝ๋๋ฅผ ๋ค์์ผ๋ก ๋ณ๊ฒฝํฉ๋๋ค.
require('socket.io-client')('wss://myserver:port/ws', { transports: ['websocket'], rejectUnauthorized: false });
ํ์ง๋ง ์ง๊ธ์ 404 ์ค๋ฅ ์๋ต์ ๋ฐ์ต๋๋ค. ๋ก๊ทธ์ ๋ด ์์ฒญ URL์ ์ถ๊ฐ /socket.io/
์ถ๊ฐ๋ ๊ฒ์ผ๋ก ํ์๋ฉ๋๋ค.
{ Error: unexpected server response (404)
at ClientRequest._req.on (/Users/chris/Projects/nationsky/mgmt-server/backend/node_modules/ws/lib/WebSocket.js:650:26)
at emitOne (events.js:115:13)
at ClientRequest.emit (events.js:210:7)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:564:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:116:23)
at TLSSocket.socketOnData (_http_client.js:453:20)
at emitOne (events.js:115:13)
at TLSSocket.emit (events.js:210:7)
at addChunk (_stream_readable.js:252:12)
at readableAddChunk (_stream_readable.js:239:11)
type: 'error',
target:
WebSocket {
domain: null,
_events: [Object],
_eventsCount: 4,
_maxListeners: undefined,
readyState: 0,
bytesReceived: 0,
extensions: {},
protocol: '',
_binaryType: 'arraybuffer',
_finalize: [Function: bound finalize],
_finalizeCalled: false,
_closeMessage: null,
_closeTimer: null,
_closeCode: null,
_receiver: null,
_sender: null,
_socket: null,
_ultron: null,
protocolVersion: 13,
_isServer: false,
url: 'wss://myserver:port/socket.io/?access_token=HnbtDUEWu1i1HVrAn-DO9YOL6GZxoittWAf8Sz_RR0s&EIO=3&transport=websocket',
_req: [Object] } } }
๋๋ฅผ ์ํด ๋๋ฒ๊ฑฐ๊ฐ ์กด์ฌํ์ง ์์๋ ๋ํ๋ฉ๋๋ค.
์๋ ! Node.js ์ฑ์์ ์๋ฒ๋ก ์ฐ๊ฒฐํ๋ ๋์ ๋์ผํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
const socket = require('socket.io-client')('https://domain.com')
DEBUG=engine* npm run dev
์คํํ ๋ก๊ทธ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
engine.io-client:socket creating transport "polling" +2s
engine.io-client:polling polling +1ms
engine.io-client:polling-xhr xhr poll +0ms
engine.io-client:polling-xhr xhr open GET: https://domain.com/socket.io/?EIO=3&transport=polling&t=LzBM71r&b64=1 +0ms
engine.io-client:polling-xhr xhr data null +0ms
engine.io-client:socket setting transport polling +1ms
engine.io-client:socket socket error {"type":"TransportError","description":503} +7ms
engine.io-client:socket socket close with reason: "transport error" +0ms
engine.io-client:polling transport not open - deferring close +0ms
๋ธ๋ผ์ฐ์ ์์ https://domain.com/socket.io/socket.io.js
์ฐ๊ฒฐํ๋ ค๊ณ ํ๋ฉด ์๋ํ๊ณ io('https://domain.com')
๋ ์ฌ์ฉํ๋ฏ๋ก ์ฃผ์์ ์ง์นจ์ ๋ฐ๋ฅด๊ณ ๋ด ์ฝ๋๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ๋ณ๊ฒฝํ์ต๋๋ค.
const socket = require('socket.io-client')('https://domain.com', { rejectUnauthorized: false })
๊ทธ๋ฆฌ๊ณ ์๋ํ์ง๋ง ์ด์ํฉ๋๋ค. Let's Encrypt์ ์ ํจํ ์ธ์ฆ์๊ฐ ์์ต๋๊น? ์๋๋ฉด ์ ์์
๋๊น? ์ด์จ๋ .. websocket
์ด์ธ์ ๋ค๋ฅธ ์ฐ๊ฒฐ์ ํผํ๊ธฐ ์ํด transports
์ต์
(์ฃผ์์์๋)์ ์ถ๊ฐํ์ผ๋ฉฐ ๊ฒฐ๊ตญ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
const socket = require('socket.io-client')('https://domain.com', {
transports: ['websocket'],
rejectUnauthorized: false
})
๋๋ถ์ @ibrahimduran ๋ฐ @SupremeTechnopriest : ๋ฏธ์๋ฅผ :
Tarzan์ฒ๋ผ ์ผ๋ค๋ฉด ์ฃ์กํฉ๋๋ค :sweat_smile: ์์ด๋ก ์์ ์ค์ ๋๋ค ํํ
๋๋์ด ๋ฌธ์ ๊ฐ ์์๊ณ ์ด๊ฒ์ ๋ํด 2 ์๊ฐ์ ๋ณด๋์ต๋๋ค. ์ต์ ์ ์ฌ๋ฐ๋ฅด๊ฒ ์ง์ ํ๋ ๊ฒ๋ฟ์ ๋๋ค. ์ด๋ฅผ ์ง์ ํ๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
let socketClient = socketIOClient.connect(`http://localhost:3001`,{ // [1] Important as fuck
reconnectionDelay: 1000,
reconnection:true,
reconnectionAttempts: 10,
transports: ['websocket'],
agent: false, // [2] Please don't set this to true
upgrade: false,
rejectUnauthorized: false
});
[1] http ๋๋ https ๋ฅผ ์ฌ์ฉํ ์ง ์ง์ ํ์ง ์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ์ https ์ ๋๋ค. ๋๋ ๋น์ ์ ๊ฐ์ URL ํ ํฌํธ๋ฅผ ์ค๋ฅธ์ชฝ์ผ๋ก ์ง์ ํด๋ฌ๋ผ๊ณ ๊ฒฝ์ฐ "URL. co.kr์์ : ํฌํธ "ํ๋กํ ์ฝ์ ๋ฐ๋ผ 80 ๋๋ 443์ ๊ธฐ๋ณธ๊ฐ์. options ๊ฐ์ฒด์์ ์ต์ ์ผ๋ก ์ง์ ๋ ํฌํธ๊ฐ ์๋ ์ผ๋ถ stackoverflow ๋ต๋ณ์ ๋ณด์์ต๋๋ค. ์ฝ๋๋ฅผ ์ดํด๋ณด๋ ๊ทธ๋ฐ ์ต์ ์ด ์์ต๋๋ค. URL์ ํฌํธ๋ฅผ ํฌํจํด์ผ ํฉ๋๋ค.
[2] ๋ํ ์์ด์ ํธ๋ฅผ true๋ก ์ค์ ํ๋๋ฐ ws ์ฐ๊ฒฐ ์ค์ ์ ์คํจํ๊ณ ์ค๋ฅ๊ฐ ์์๊ณ ๋จ์ํ ์๊ฐ ์ด๊ณผ๋์์ต๋๋ค. ์์ด์ ํธ๋ฅผ ์ ํ ์ง์ ํ์ง ๋ง์ญ์์ค. ๊ธฐ๋ณธ๊ฐ์ false์ ๋๋ค.
@ teja42 2์ผ ๋์ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ค๊ณ ํ์ต๋๋ค. ๋๋ ๊ทธ๊ฒ์ด webpack๊ณผ์ ๋ฒ๋ค ๋ฌธ์ ๋ผ๊ณ ์๊ฐํ์ง๋ง ๋น์ ์ ์๋ฃจ์ ์ ํ๋ฅญํ๊ฒ ์๋ํ์ต๋๋ค. ๊ฐ์ฌ ํด์!
์๋ !
๋๋ ๊ฐ์ ๋ฌธ์ ๊ฐ ์์๊ณ @SupremeTechnopriest๊ฐ ๋งํ๋๋ก 2.0.1์์ 1.7.4๋ก ๋ค์ด ๊ทธ๋ ์ด๋๋ฅผ ์์ ํ์ผ๋ฉฐ ๋ชจ๋ ๊ฒ์ด ์ ์๋ํฉ๋๋ค!
๊ฐ์ฌ !
์๋ ํ์ธ์, Android 8.1์์ ์ฌ์ ํ ์ด ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค.
์์ฌ ์ฝ๋:
```
this.socket = io.connect('https://xxxxxxxxxxxxxx', {
๊ฒฝ๋ก: '/์์ผ ์ฑ๋',
์ฌ์ฐ๊ฒฐ ์ง์ฐ: 1000,
์ฌ์ฐ๊ฒฐ: ์ฌ์ค,
์ฌ์ฐ๊ฒฐ ์๋: 10,
์ ์ก: ['์น ์์ผ'],
์์ด์ ํธ: ๊ฑฐ์ง,
์
๊ทธ๋ ์ด๋: ๊ฑฐ์ง,
๊ฑฐ๋ถ ๊ถํ ์์: ๊ฑฐ์ง
});
this.socket.on('์ฐ๊ฒฐ', () => {
console.log('์์ผ์ด ์ฐ๊ฒฐ๋์์ต๋๋ค!')
})
this.socket.on('connect_error', (์ค๋ฅ) => {
console.log('์์ผ ์ฐ๊ฒฐ ์ค๋ฅ --> ' + err);
})
```
์์ฉ ํ๋ก๊ทธ๋จ์ 'connection_error' ์ด๋ฒคํธ๋ก ์ด๋ํ๊ณ "์ค๋ฅ: websocket ์ค๋ฅ"๋ฅผ ๊ธฐ๋กํฉ๋๋ค.
"๋ฐ์ ๋ค์ดํฐ๋ธ": "0.57.4",
"socket.io-client": "1.7.4"
์ด๋ค ์ ์?
์์ฉ ํ๋ก๊ทธ๋จ์ 'connection_error' ์ด๋ฒคํธ๋ก ์ด๋ํ๊ณ "์ค๋ฅ: websocket ์ค๋ฅ"๋ฅผ ๊ธฐ๋กํฉ๋๋ค.
๊ฐ์ ๋ฌธ์ ์ ์ง๋ฉด
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
"engine.io-client": "~3.3.2",
"socket.io-client": "2.1.1",
๋์ผํ ๋ฌธ์ ์ ์ง๋ฉดํ๊ณ ์์ต๋๋ค. ์ ๋ฐ์ดํธ๊ฐ ์์ต๋๊น?
+1
์์ผ ์๋ฒ์ origins
๋๋ฌธ์ ์ด ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.
์ด์ ์ถ์ฒ๋ฅผ ์ถ๊ฐํ๋ ๋ฐฉ๋ฒ์ ๋ฌด์์
๋๊น?
์๋ ๋ชจ๋. ๊ทธ๋์ ๋ ธ๋ ์ต์คํ๋ ์ค ์๋ฒ์ PM2 ํด๋ฌ์คํฐ๋ฅผ ์ฌ์ฉํ๊ณ redis ์ด๋ํฐ๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค.
const redis = require('socket.io-redis')
io.adapter(redis(config.redis))
๋ธ๋ผ์ฐ์ ํด๋ผ์ด์ธํธ์ ๊ฒฝ์ฐ ์ด๊ฒ์ ๊ด์ฐฎ์ ๋ณด์ด์ง๋ง socket.io-client์ ๊ฒฝ์ฐ ๋ค์์ ์ป์ต๋๋ค.
{ Error: xhr post error
at XHR.Transport.onError (/home/user/Desktop/nodeclienttest/node_modules/engine.io-client/lib/transport.js:67:13)
at Request.<anonymous> (/home/user/Desktop/nodeclienttest/node_modules/engine.io-client/lib/transports/polling-xhr.js:111:10)
at Request.Emitter.emit (/home/user/Desktop/nodeclienttest/node_modules/component-emitter/index.js:133:20)
at Request.onError (/home/user/Desktop/nodeclienttest/node_modules/engine.io-client/lib/transports/polling-xhr.js:311:8)
at Timeout._onTimeout (/home/user/Desktop/nodeclienttest/node_modules/engine.io-client/lib/transports/polling-xhr.js:258:18)
at listOnTimeout (timers.js:327:15)
at processTimers (timers.js:271:5) type: 'TransportError', description: 400 }
{ Error: xhr poll error
at XHR.Transport.onError (/home/user/Desktop/nodeclienttest/node_modules/engine.io-client/lib/transport.js:67:13)
at Request.<anonymous> (/home/user/Desktop/nodeclienttest/node_modules/engine.io-client/lib/transports/polling-xhr.js:130:10)
at Request.Emitter.emit (/home/user/Desktop/nodeclienttest/node_modules/component-emitter/index.js:133:20)
at Request.onError (/home/user/Desktop/nodeclienttest/node_modules/engine.io-client/lib/transports/polling-xhr.js:311:8)
at Timeout._onTimeout (/home/user/Desktop/nodeclienttest/node_modules/engine.io-client/lib/transports/polling-xhr.js:258:18)
at listOnTimeout (timers.js:327:15)
at processTimers (timers.js:271:5) type: 'TransportError', description: 400 }
์น ์์ผ ์ ์ก์ ์ฌ์ฉํ๋๋ก ํด๋ผ์ด์ธํธ ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ๋ฉด ๋ชจ๋ ์ ๋๋ก ์๋ํฉ๋๋ค.
var socket = require("socket.io-client")("http://localhost:8080", { transports: ['websocket'] });
๋ฌธ์ ๋ ์ด ์ฝ๋๋ฅผ ๋ชจ๋ ํด๋ผ์ด์ธํธ ์ฝ๋์ ๋ณ๊ฒฝํ๋๋ก ์๊ตฌํ๊ณ ์ถ์ง ์๋ค๋ ๊ฒ์ ๋๋ค.
๋ด ๊ถ์ฅ ์ฌํญ์ xhr ํด๋ง ์ ์ก์ด ์คํจํ๋ ๊ฒฝ์ฐ ์น ์์ผ ์ ์ก์ ์๋ํ๋๋ก socket.io-client๋ฅผ ๋ณ๊ฒฝํ๋ ๊ฒ์ ๋๋ค. ์ด๋ป๊ฒ ์๊ฐํด?
๋ค์๊ณผ ๊ฐ์ด github์์ ๋ง์ง๋ง ๋ฒ์ ์ ์๋ํ๊ณ RN android ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ต๋๋ค.
npm i socketio/engine.io-client#3.3.2 -S
๊ฑด๋ฐฐ
์ด ์๋ฃจ์ ์ด ์ฝ๊ฐ ์ด์ํ๊ฒ ๋ค๋ฆด ๊ฒ์ด๋ผ๋ ๊ฒ์ ์๊ณ ์์ง๋ง ์ด ์ค๋ฅ๋ฅผ ์ธํฐ๋ท ๊ฒ์ํ์ฌ ์ฌ๊ธฐ๊น์ง ์์ต๋๋ค.
์ ๊ฒฝ์ฐ์๋ ์๋ฒ๊ฐ ์คํ๋์ง ์์๊ธฐ ๋๋ฌธ์ ์ด ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค(๊ทธ๋ ๋ค๊ณ ์๊ฐํ์ต๋๋ค). ๊ทธ๊ฒ์ด ๋ค๋ฅธ ๋๊ตฐ๊ฐ๋ฅผ ๋๊ธฐ๋ฅผ ๋ฐ๋๋๋ค :)
์ด ์๋
require('socket.io-client')('http://localhost', { rejectUnauthorized: false, })
๊ฐ์ฌํฉ๋๋ค. ๋ ธ๋ ์์ฉ ํ๋ก๊ทธ๋จ์์ ๋ด ์๋ฒ์ ์ฐ๊ฒฐํ๋ ๋ฐ ๋ฌธ์ ๊ฐ ์์์ต๋๋ค. ์ด ์๋ฃจ์ ์ ์ ์๊ฒ ํจ๊ณผ์ ์ด์์ต๋๋ค.
์ด ์๋
require('socket.io-client')('http://localhost', { rejectUnauthorized: false, })
๊ฐ์ฌํฉ๋๋ค. ๋ ธ๋ ์์ฉ ํ๋ก๊ทธ๋จ์์ ๋ด ์๋ฒ์ ์ฐ๊ฒฐํ๋ ๋ฐ ๋ฌธ์ ๊ฐ ์์์ต๋๋ค. ์ด ์๋ฃจ์ ์ ์ ์๊ฒ ํจ๊ณผ์ ์ด์์ต๋๋ค.
์๋ ํ์ธ์, ์ด๋ค ๋ฒ์ ์ socket.io์ socket.io-client๋ฅผ ์ฌ์ฉํ๊ณ ์๋์ง ๊ถ๊ธํฉ๋๋ค.
์ด ์๋
require('socket.io-client')('http://localhost', { rejectUnauthorized: false, })
๊ฐ์ฌํฉ๋๋ค. ๋ ธ๋ ์์ฉ ํ๋ก๊ทธ๋จ์์ ๋ด ์๋ฒ์ ์ฐ๊ฒฐํ๋ ๋ฐ ๋ฌธ์ ๊ฐ ์์์ต๋๋ค. ์ด ์๋ฃจ์ ์ ์ ์๊ฒ ํจ๊ณผ์ ์ด์์ต๋๋ค.
์๋ ํ์ธ์, ์ด๋ค ๋ฒ์ ์ socket.io์ socket.io-client๋ฅผ ์ฌ์ฉํ๊ณ ์๋์ง ๊ถ๊ธํฉ๋๋ค.
๋ฒ์ 2.3.0์์ socket.io์ socket.io-client๋ฅผ ๋ชจ๋ ์ฌ์ฉํ๊ณ ์์ต๋๋ค.
์๋ ํ์ธ์, Android 8.1์์ ์ฌ์ ํ ์ด ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค.
์์ฌ ์ฝ๋:
this.socket = io.connect('https://xxxxxxxxxxxxxx', { path: '/socketchannel', reconnectionDelay: 1000, reconnection:true, reconnectionAttempts: 10, transports: ['websocket'], agent: false, upgrade: false, rejectUnauthorized: false }); this.socket.on('connect', () => { console.log('socket connected!') }) this.socket.on('connect_error', (err) => { console.log('socket connected error --> ' + err); })
์์ฉ ํ๋ก๊ทธ๋จ์ 'connection_error' ์ด๋ฒคํธ๋ก ์ด๋ํ๊ณ "์ค๋ฅ: websocket ์ค๋ฅ"๋ฅผ ๊ธฐ๋กํฉ๋๋ค.
"๋ฐ์ ๋ค์ดํฐ๋ธ": "0.57.4",
"socket.io-client": "1.7.4"์ด๋ค ์ ์?
๊ฐ์ ๋ฌธ์ ์ ๋๋ค. ํด๊ฒฐํ์๊ฒ ์ต๋๊น?
๋๋ฉ์ธ ์ด๋ฆ์ ์ฌ์ฉํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํฉ๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ผ์ฃผ์ผ ์ ์ ๋ฆฌํฉํ ๋งํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ง๋ง ์ ํํ ๋ฌด์์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋์ง ๊ธฐ์ต๋์ง ์์ต๋๋ค. ์ฃ์กํฉ๋๋ค. ๋๋ ๋ฐ์ ํ ์ฃผ๋ฅผ ๋ณด๋๊ณ ๊ทธ๊ฒ์ด ์ด๊ธฐ ์ปค๋ฐ์ด์์ต๋๋ค.
ํด๋ผ์ด์ธํธ์์
opts.transports
๋ฅผwebsocket
๋ก๋ง ์ค์ ํ ์ ์์ต๋๋ค.์ฝ๋๋ฅผ ๋ฆฌํฉํ ๋งํ๊ธฐ ์ ์ ๋๋ฌด ๋ณต์กํ ์์ผ ๋ฐ ์์ ๋ฐ์ธ๋ฉ ๋ ผ๋ฆฌ๊ฐ ์์์ต๋๋ค. ๋ฐ๋ผ์ ์ฝ๋์ ์์ผ ๋ ผ๋ฆฌ๊ฐ ์๋ํ๋์ง ํ์ธํ๊ณ ๋๋ฒ๊ทธ๋ฅผ ๋ ๊ฐ๋จํ๊ฒ ๋ง๋์ญ์์ค.