๋น React Native Expo ํ๋ก์ ํธ์ socket.io-client
๋ฅผ ์ค์นํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๋
ธ๋์ ๋ฉ์์ง๊ฐ ํ์๋ฉ๋๋ค.
Unrecognized WebSocket connection option(s) `agent`, `perMessageDeflate`, `pfx`, `key`, `passphrase`, `cert`, `ca`, `ciphers`, `rejectUnauthorized`. Did you mean to put these under `headers`?
yarn add socket.io-client
React Native ํ๋ก์ ํธ์ App.js์์ :
import React from 'react';
import { YellowBox } from 'react-native';
import io from 'socket.io-client';
YellowBox.ignoreWarnings(['Remote debugger']);
import AppContainer from './config/routing';
export default class App extends React.Component {
constructor(props) {
super(props);
}
componentWillMount() {
const socket = io('http://10.0.60.26:3000', {
forceNew: true
});
socket.on('connect', () => console.log('Connection'));
}
render() {
return <AppContainer />;
}
}
๊ฐ์ ๋ฌธ์ :(
+1
+1
+1
์ ์ด๋ 12 ์ 18 ์ผ๋ถํฐ ๋ฌธ์ ๊ฐ ๋ ๊ฒ ๊ฐ์ต๋๋ค.
+1
+1
+1
+1
React-Native์์ socket.io (2.2.0)๋ฅผ ์ฌ์ฉํ ๋ ์ฌ์ ํ์ด ๊ฒฝ๊ณ ๊ฐ ํ์๋ฉ๋๋ค. ๋ค์์ ์ฌ์ฉํ์ฌ ์ฅ์น ํ๋ฉด์ ๋ ธ๋์ ์์๋ฅผ ์์๊ฑฐ ํ ์ ์์ต๋๋ค.
import { YellowBox } from 'react-native'
YellowBox.ignoreWarnings([
'Unrecognized WebSocket connection option(s) `agent`, `perMessageDeflate`, `pfx`, `key`, `passphrase`, `cert`, `ca`, `ciphers`, `rejectUnauthorized`. Did you mean to put these under `headers`?'
])
ํ์ง๋ง ์ฌ์ ํ ์ฝ์์์ด ์ฑ๊ฐ์ ๊ฒฝ๊ณ ๊ฐ ํ์๋ฉ๋๋ค ...
"socket.io-client"๋ก ๋ค์ด ๊ทธ๋ ์ด๋ : "2.1.0"๋ฐ ๋ชจ๋ OK
๋๋ฅผ ์ํด 2.1.1๋ก ๋ค์ด ๊ทธ๋ ์ด๋ํ์ ๋ ์๋ํ์ต๋๋ค.
yarn add [email protected]
์ฒ์์๋ ํจํค์ง์ ๋ค๋ฅธ ๋ถ ๋ฒ์ ์ ํ์ฉํ๋ socket.io-client@^2.1.1
๋ฒ์ ์์ ^
๊ธฐํธ๋ฅผ ์ถ๊ฐํ๊ธฐ ๋๋ฌธ์ ์๋ํ์ง ์์์ต๋๋ค.
์ฌ์ ํ ๋ฌธ์ @ 2.2.0
socket.io-client๋ฅผ 1.7.2๋ก ๋ค์ด ๊ทธ๋ ์ด๋ ํด๋ณด์ญ์์ค.
์ฌ์ ํ ๋ฌธ์ @ ^ 2.3.0
react-native์์ ์ฌ์ ํ v2.3.0
๋ฌธ์
@ 2.1.1์ ๊ด์ฐฎ์ต๋๋ค. ๊ทธ๋ฌ๋ 2.3.0์ RN 0.61.1์ ๊ฒฝ๊ณ ๋ฉ์์ง๋ฅผ ํ์ํฉ๋๋ค.
์ง๊ธ๊น์ง ์ด๋ค ํด๊ฒฐ์ฑ ๋
๋ฐ์ ๋ค์ดํฐ๋ธ์ ํจ๊ป ์ฌ์ ํ ๋ฌธ์ v2.3.0
์๋ค ์! v2.1.1 ๋ก ๋ค์ด ๊ทธ๋ ์ด๋ํ์ฌ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ต๋๋ค.
์ด์ ๋ฒ์ ์ ์์ ํ ์ ๊ฑฐํ๊ณ ์บ์๋ฅผ ์ง์์ผํฉ๋๋ค.
๋๋! ์ด๊ฒ์ react-native ๋ฐ react-native-web์์ ์๋ํฉ๋๋ค!
๋๋ react-native: 0.61.4
์ ๋์ผํ ๋ฌธ์ ๊ฐ ์์ง๋ง ์ด๋ป๊ฒ ๋ ์ฑ์ด ์ถฉ๋ํ๊ณ ๊ฒฝ๊ณ ๋ก ์ธํด ๋ฐ์ํ๋์ง ๋๋ ๋ค๋ฅธ ์์ผ ๊ด๋ จ ๋ฌธ์ ์ธ์ง ์ ์ ์์ง๋ง ์์ผ์ ์ฐ๊ฒฐํ๋ ค๊ณ ํ ๋ ์ฑ์ด ๋ซํ๋๋ค.
์ด๊ฒ์ ๋ด conf์
๋๋ค.
{
autoConnect: false,
reconnectionDelay: 1000,
reconnection: true,
transports: ['websocket'],
agent: false, // [2] Please don't set this to true
upgrade: false,
rejectUnauthorized: false
}
์์ผ์ด ์ด๋ฏธ ์ฐ๊ฒฐ๋ ๊ฒฝ์ฐ
socket.open
์ด (๊ฐ) ์ถฉ๋ํฉ๋๊น?
์ด ๊ฒฝ๊ณ ๋ ์ฌ๊ธฐ์์ ReactNative/WebSocket.js:117
์ํด ๋ฐ์ํฉ๋๋ค. EngineIO (๋ด๋ถ์์ SocketIO์ ์ํด ์ฌ์ฉ๋จ)๋ ๋ค์ ์์ฑ์ ํฌํจํ๋ opts
์ ๋ฌ๋๋ EngineIO/websocket.js:121
๋ฅผ ํตํด ๋
ธ์ถ ๋ ReactNative WebSocket ๊ตฌํ์ ์ฐธ์กฐํ๊ธฐ ๋๋ฌธ์
๋๋ค. ๊ฒฝ๊ณ ๋ฉ์์ง๊ฐ ์ธ๊ธ๋ฉ๋๋ค. ํฅ๋ฏธ๋กญ๊ฒ๋ EngineIO ๊ด๋ฆฌ์๊ฐ ์ฃผ์์ ํตํด ์ค๋ช
ํ๋ ์ด๋ฌํ ์ถ๊ฐ ์์ฑ์ "Node.js ํด๋ผ์ด์ธํธ ์ฉ SSL ์ต์
"์
๋๋ค.
์์ ๋ค์๊ณผ ๊ฐ์ ์ฝ๋๋ฅผ ์ฝ์
ํ์ฌ EngineIO/websocket.js:114
์กฐ์ํ๋ opts
์ฐ๋ฆฌ๊ฐ ๋ชจ๋ ์ธ๋ถ ํค๋ฅผ ์ ๊ฑฐํ๋ ค๋ฉด ์น ์์ผ ์์ฑ์์ ์ฃผ์ด์ง opts
์ฑ๊ณต์ ์ผ๋ก ๋ํ๋๋ ๊ฒฝ๊ณ ๋ฅผ ์ค์ง๋ฅผ 1 ๋ฑ ( omit
์ Lodash์์ ๊ฐ์ ธ ์ค๋ฉฐ ๋ค์ ์์
ํ ์ ์์).
// lib/transports/websocket.js
...
if (this.isReactNative) {
// prettier-ignore
opts = omit(opts, ['agent', 'perMessageDeflate', 'pfx', 'key', 'passphrase', 'cert', 'ca', 'ciphers', 'rejectUnauthorized']);
}
...
๋น์ ์ ๋จ์ํ ํตํด ๊ฒฝ๊ณ ์์๊ฑฐ ํฌํจํ์ง ์๋ ์๋ฃจ์
์ ์ฐพ๊ณ ์๋ค๋ฉด YellowBox.ignoreWarnings
๋น์ ์ ์์ ํ ๋ฌด์ํ ์ doOpen
์ ์ EngineIO/websocket.js:87
์ด ์๋์ ๊ฐ์ด ๋ญ๊ฐ๋ฅผ ์ถ๊ฐํ์ฌ , ReactNative ํ๋ก์ ํธ์ ์ง์
์ (์ผ๋ฐ์ ์ผ๋ก index.js
ํฉ๋๋ค.
// index.js
...
+ import 'patches/EngineIOHeaderWarning';
...
// patches/EngineIOHeaderWarning.js
const WS = require('engine.io-client/lib/transports/websocket');
var WebSocketImpl = WebSocket; // eslint-disable-line no-undef
WS.prototype.doOpen = function() {
if (!this.check()) {
// let probe timeout
return;
}
var uri = this.uri();
var protocols = this.protocols;
var opts = {};
if (this.extraHeaders) {
opts.headers = this.extraHeaders;
}
if (this.localAddress) {
opts.localAddress = this.localAddress;
}
try {
this.ws = new WebSocketImpl(uri, protocols, opts);
} catch (err) {
return this.emit('error', err);
}
};
์ ๋ฐ์ ์ผ๋ก, this.isReactNative
๋ฅผ ํตํด ReactNative์์ ์ฌ์ฉํ ๋ ํ์ํ์ง ์์ ์ถ๊ฐ opts
๋ฅผ ์ ์ธํ๊ธฐ ์ํด EngineIO์ ๋ํ pull ์์ฒญ์ ์์ฑํ๋ฉด ํด๋น ์์ผ์์ SocketIO ํด๋ผ์ด์ธํธ๋ฅผ ์ฌ์ฉํ๋ ๋ชจ๋ ์ฌ์ฉ์์ ๋ํด์ด ๊ฒฝ๊ณ ๋ฅผ ์ต์ ํ ์ ์์ต๋๋ค. ReactNative ํ๋ก์ ํธ.
๋น์ ์ :
- [x] _bug_ ์ ๊ณ
- [] _feature_ ์์ฒญ
ํ์ฌ ํ๋
๋น React Native Expo ํ๋ก์ ํธ์
socket.io-client
๋ฅผ ์ค์นํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๋ ธ๋์ ๋ฉ์์ง๊ฐ ํ์๋ฉ๋๋ค.Unrecognized WebSocket connection option(s) `agent`, `perMessageDeflate`, `pfx`, `key`, `passphrase`, `cert`, `ca`, `ciphers`, `rejectUnauthorized`. Did you mean to put these under `headers`?
์ฌํ ๋จ๊ณ (ํ์ฌ ๋์์ด ๋ฒ๊ทธ ์ธ ๊ฒฝ์ฐ)
yarn add socket.io-client
React Native ํ๋ก์ ํธ์ App.js์์ :
import React from 'react'; import { YellowBox } from 'react-native'; import io from 'socket.io-client'; YellowBox.ignoreWarnings(['Remote debugger']); import AppContainer from './config/routing'; export default class App extends React.Component { constructor(props) { super(props); } componentWillMount() { const socket = io('http://10.0.60.26:3000', { forceNew: true }); socket.on('connect', () => console.log('Connection')); } render() { return <AppContainer />; } }
๋ด ์๋ฃจ์ :
๋ค์๊ณผ ๊ฐ์ด useState์ ์์ผ์ ์ ์ฅํ์ญ์์ค.
'socket.io-client'์์ io ๊ฐ์ ธ ์ค๊ธฐ;
// ...
const [socket] = useState(() => io('SERVER_URL'));
๋ฌธ์ ํด๊ฒฐ๋จ.
์ ๊ฒฝ์ฐ์๋ ๋ก์ปฌ socket.io ์๋ฒ์ ์ฐ๊ฒฐํ ๋ v2.3์ด ๊ด์ฐฎ์ต๋๋ค.
ํ์ง๋ง ์๊ฒฉ socket.io ์๋ฒ์ ์ฐ๊ฒฐํ ๋ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
์๋ฒ ์์ค๊ฐ ๋์ผํฉ๋๋ค.
๊ทธ๋์ socket.io ํด๋ผ์ด์ธํธ ๋ฒ์ ์ 2.1.1๋ก ๋ค์ด ๊ทธ๋ ์ด๋ํ๋ฉด ์ ์์ ์ผ๋ก ์๋ํฉ๋๋ค.
์ด ๊ฒฝ๊ณ ๋ ์ฌ๊ธฐ์์
ReactNative/WebSocket.js:117
์ํด ๋ฐ์ํฉ๋๋ค. EngineIO (๋ด๋ถ์์ SocketIO์ ์ํด ์ฌ์ฉ๋จ)๋ ๋ค์ ์์ฑ์ ํฌํจํ๋opts
์ ๋ฌ๋๋EngineIO/websocket.js:121
๋ฅผ ํตํด ๋ ธ์ถ ๋ ReactNative WebSocket ๊ตฌํ์ ์ฐธ์กฐํ๊ธฐ ๋๋ฌธ์ ๋๋ค. ๊ฒฝ๊ณ ๋ฉ์์ง๊ฐ ์ธ๊ธ๋ฉ๋๋ค. ํฅ๋ฏธ๋กญ๊ฒ๋ EngineIO ๊ด๋ฆฌ์๊ฐ ์ฃผ์์ ํตํด ์ค๋ช ํ๋ ์ด๋ฌํ ์ถ๊ฐ ์์ฑ์ "Node.js ํด๋ผ์ด์ธํธ ์ฉ SSL ์ต์ "์ ๋๋ค.์์ ๋ค์๊ณผ ๊ฐ์ ์ฝ๋๋ฅผ ์ฝ์ ํ์ฌ
EngineIO/websocket.js:114
์กฐ์ํ๋opts
์ฐ๋ฆฌ๊ฐ ๋ชจ๋ ์ธ๋ถ ํค๋ฅผ ์ ๊ฑฐํ๋ ค๋ฉด ์น ์์ผ ์์ฑ์์ ์ฃผ์ด์งopts
์ฑ๊ณต์ ์ผ๋ก ๋ํ๋๋ ๊ฒฝ๊ณ ๋ฅผ ์ค์ง๋ฅผ 1 ๋ฑ (omit
์ Lodash์์ ๊ฐ์ ธ ์ค๋ฉฐ ๋ค์ ์์ ํ ์ ์์).// lib/transports/websocket.js ... if (this.isReactNative) { // prettier-ignore opts = omit(opts, ['agent', 'perMessageDeflate', 'pfx', 'key', 'passphrase', 'cert', 'ca', 'ciphers', 'rejectUnauthorized']); } ...
๋น์ ์ ๋จ์ํ ํตํด ๊ฒฝ๊ณ ์์๊ฑฐ ํฌํจํ์ง ์๋ ์๋ฃจ์ ์ ์ฐพ๊ณ ์๋ค๋ฉด
YellowBox.ignoreWarnings
๋น์ ์ ์์ ํ ๋ฌด์ํ ์doOpen
์ ์EngineIO/websocket.js:87
์ด ์๋์ ๊ฐ์ด ๋ญ๊ฐ๋ฅผ ์ถ๊ฐํ์ฌ , ReactNative ํ๋ก์ ํธ์ ์ง์ ์ (์ผ๋ฐ์ ์ผ๋กindex.js
ํฉ๋๋ค.// index.js ... + import 'patches/EngineIOHeaderWarning'; ...
// patches/EngineIOHeaderWarning.js const WS = require('engine.io-client/lib/transports/websocket'); var WebSocketImpl = WebSocket; // eslint-disable-line no-undef WS.prototype.doOpen = function() { if (!this.check()) { // let probe timeout return; } var uri = this.uri(); var protocols = this.protocols; var opts = {}; if (this.extraHeaders) { opts.headers = this.extraHeaders; } if (this.localAddress) { opts.localAddress = this.localAddress; } try { this.ws = new WebSocketImpl(uri, protocols, opts); } catch (err) { return this.emit('error', err); } };
์ ๋ฐ์ ์ผ๋ก,
this.isReactNative
๋ฅผ ํตํด ReactNative์์ ์ฌ์ฉํ ๋ ํ์ํ์ง ์์ ์ถ๊ฐopts
๋ฅผ ์ ์ธํ๊ธฐ ์ํด EngineIO์ ๋ํ pull ์์ฒญ์ ์์ฑํ๋ฉด ํด๋น ์์ผ์์ SocketIO ํด๋ผ์ด์ธํธ๋ฅผ ์ฌ์ฉํ๋ ๋ชจ๋ ์ฌ์ฉ์์ ๋ํด์ด ๊ฒฝ๊ณ ๋ฅผ ์ต์ ํ ์ ์์ต๋๋ค. ReactNative ํ๋ก์ ํธ.
๊ทํ์ ์ฝ๋๋ก ์ธํด ์๋ฒ์ ์ฐ๊ฒฐํ ์ ์์ผ๋ฉฐ EngineIO์ ์๋ ๋ ๋์์ด ๋ณ๊ฒฝ๋์์ผ๋ฏ๋ก ํด๋น ์ต์ ์ if ๋ฌธ์ผ๋ก ๋ํํ์ง๋ง ์ ์ํ ๊ฒ๊ณผ ๋์ผํ ํจ์น ๋ฉ์ปค๋์ฆ์ ์ฌ์ฉํ์ต๋๋ค.
// patches/EngineIOHeaderWarning.js
const WS = require('engine.io-client/lib/transports/websocket');
var WebSocketImpl = WebSocket; // eslint-disable-line no-undef
WS.prototype.doOpen = function() {
if (!this.check()) {
// let probe timeout
return;
}
var uri = this.uri();
var protocols = this.protocols;
var opts = {};
if(!this.isReactNative){
opts.agent = this.agent;
opts.perMessageDeflate = this.perMessageDeflate;
// SSL options for Node.js client
opts.pfx = this.pfx;
opts.key = this.key;
opts.passphrase = this.passphrase;
opts.cert = this.cert;
opts.ca = this.ca;
opts.ciphers = this.ciphers;
opts.rejectUnauthorized = this.rejectUnauthorized;
}
if (this.extraHeaders) {
opts.headers = this.extraHeaders;
}
if (this.localAddress) {
opts.localAddress = this.localAddress;
}
try {
this.ws =
this.usingBrowserWebSocket && !this.isReactNative
? protocols
? new WebSocketImpl(uri, protocols)
: new WebSocketImpl(uri)
: new WebSocketImpl(uri, protocols, opts);
} catch (err) {
return this.emit('error', err);
}
if (this.ws.binaryType === undefined) {
this.supportsBinary = false;
}
if (this.ws.supports && this.ws.supports.binary) {
this.supportsBinary = true;
this.ws.binaryType = 'nodebuffer';
} else {
this.ws.binaryType = 'arraybuffer';
}
this.addEventListeners();
};
"socket.io-client"๋ก ๋ค์ด ๊ทธ๋ ์ด๋ : "2.1.0"๋ฐ ๋ชจ๋ OK
์ด๊ฒ์ ๋๋ฅผ ์ํด ์ผํ์ต๋๋ค.
๋ฌธ์ ๋ฅผ ์ ์ดํดํ๊ณ ์๋ค๋ฉด https://github.com/socketio/socket.io-client/commit/06e9a4ca2621176c30c352b2ba8b34fa42b8d0ba์ ๋์ ๋ ๊ด๋ จ ์ข ์์ฑ ๋ฌธ์
2.1.1 ๋ก์ ๋ค์ด ๊ทธ๋ ์ด๋๋ ์ค์ ๋ก ์๋ํ์ง๋ง 2 ๋ ๋ ๋ฆด๋ฆฌ์ค์ ๊ฐํ ์์ต๋๋ค.
@ptboyer๋ฅผ ํ๋ก์ ํธ์ ์ ์ฉํ๊ณ ๋ฆด๋ฆฌ์ค ํ ์ ์์ต๋๊น?
์ด ๊ฒฝ๊ณ ๊ฐ ํจํค์ง์ ๊ธฐ๋ฅ์ ์ํฅ์ ์ค๋๊น? ๊ทธ๋ฅ ์ค์ ํ๊ณ ์ด๊ฒ์ ์ฝ๋งค์ด๊ณ ์ถ์ง ์์ต๋๋ค. ๋ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ ์์ผ๋ฉด ๋ฌด์ํด๋ ์ข์ต๋๋ค.
ํธ์ง : ๊ฐ์ ๊ฒ์ ๊ถ๊ธํดํ๋ ์ฌ๋์ ์ํด. ๋ด๊ฐ ๋งํ ์์๋ ํ์ด ๊ฒฝ๊ณ ๋ ์ค์ํ์ง ์์ต๋๋ค.
@schumannd ์ด ๋ฌธ์ ๋ ์์ ํ ์ค๋ฅ / ๊ฒฝ๊ณ ๋ฉ์์ง ์์๊ฑฐ์ ๊ดํ ๊ฒ์ ๋๋ค. ๊ธฐ๋ฅ์ ์ผ๋ก์ด ๊ฒฝ๊ณ ๋ ์ค์ํ์ง ์์ต๋๋ค. :)
+1
๋๋ socket.io ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ฌ์ ํ ์ ์ง๋๊ณ ์๋์ง ์๋์ง์ ๋ํ ๊ณต์์ ์ธ ๋ต๋ณ์ด๋ ๋ค๊ฐ์ค๋ ์ ๋ฐ์ดํธ๋ก ์ธํด ๋งค์ฐ ๊ฑฑ์ ๋ฉ๋๋ค.
+1
+1
const _io = ioClient.connect(socketurl, { forceNode: true });
์ผ์ด์ผ
@isthaison ๊ทธ๊ฒ์ ๋๋ฅผ ์ํด ์๋ํ์ง ์์ต๋๋ค
@isthaison ์ด ์๋ํ์ง๋ง ๋ค๋ฅธ ๋ชจ๋ http ์์ฒญ์ ๋งค์ฐ ๋๋ ค์ง๋๋ค ... ํด๊ฒฐ ๋ฐฉ๋ฒ๊ณผ ๊ด๋ จ์ด ์๋์ง ๋ชจ๋ฅด๊ฒ ์ต๋๋ค ...ํ์ง๋ง ...
{forceNode : true}
๊ฐ์ฌํฉ๋๋ค. ์ด๊ฒ์ ๋๋ฅผ ์ํด ์ผํฉ๋๋ค.
์ด๊ฒ์ ๋๋ฅผ ์ํด ์ผํ ์ ์ผํ ์๋ฃจ์ ์ ๋๋ค.
์ด ๊ฒฝ๊ณ ๋ ์ฌ๊ธฐ์์
ReactNative/WebSocket.js:117
์ํด ๋ฐ์ํฉ๋๋ค. EngineIO (๋ด๋ถ์์ SocketIO์ ์ํด ์ฌ์ฉ๋จ)๋ ๋ค์ ์์ฑ์ ํฌํจํ๋opts
์ ๋ฌ๋๋EngineIO/websocket.js:121
๋ฅผ ํตํด ๋ ธ์ถ ๋ ReactNative WebSocket ๊ตฌํ์ ์ฐธ์กฐํ๊ธฐ ๋๋ฌธ์ ๋๋ค. ๊ฒฝ๊ณ ๋ฉ์์ง๊ฐ ์ธ๊ธ๋ฉ๋๋ค. ํฅ๋ฏธ๋กญ๊ฒ๋ EngineIO ๊ด๋ฆฌ์๊ฐ ์ฃผ์์ ํตํด ์ค๋ช ํ๋ ์ด๋ฌํ ์ถ๊ฐ ์์ฑ์ "Node.js ํด๋ผ์ด์ธํธ ์ฉ SSL ์ต์ "์ ๋๋ค.
์์ ๋ค์๊ณผ ๊ฐ์ ์ฝ๋๋ฅผ ์ฝ์ ํ์ฌEngineIO/websocket.js:114
์กฐ์ํ๋opts
์ฐ๋ฆฌ๊ฐ ๋ชจ๋ ์ธ๋ถ ํค๋ฅผ ์ ๊ฑฐํ๋ ค๋ฉด ์น ์์ผ ์์ฑ์์ ์ฃผ์ด์งopts
์ฑ๊ณต์ ์ผ๋ก ๋ํ๋๋ ๊ฒฝ๊ณ ๋ฅผ ์ค์ง๋ฅผ 1 ๋ฑ (omit
์ Lodash์์ ๊ฐ์ ธ ์ค๋ฉฐ ๋ค์ ์์ ํ ์ ์์).// lib/transports/websocket.js ... if (this.isReactNative) { // prettier-ignore opts = omit(opts, ['agent', 'perMessageDeflate', 'pfx', 'key', 'passphrase', 'cert', 'ca', 'ciphers', 'rejectUnauthorized']); } ...
๋น์ ์ ๋จ์ํ ํตํด ๊ฒฝ๊ณ ์์๊ฑฐ ํฌํจํ์ง ์๋ ์๋ฃจ์ ์ ์ฐพ๊ณ ์๋ค๋ฉด
YellowBox.ignoreWarnings
๋น์ ์ ์์ ํ ๋ฌด์ํ ์doOpen
์ ์EngineIO/websocket.js:87
์ด ์๋์ ๊ฐ์ด ๋ญ๊ฐ๋ฅผ ์ถ๊ฐํ์ฌ , ReactNative ํ๋ก์ ํธ์ ์ง์ ์ (์ผ๋ฐ์ ์ผ๋กindex.js
ํฉ๋๋ค.// index.js ... + import 'patches/EngineIOHeaderWarning'; ...
// patches/EngineIOHeaderWarning.js const WS = require('engine.io-client/lib/transports/websocket'); var WebSocketImpl = WebSocket; // eslint-disable-line no-undef WS.prototype.doOpen = function() { if (!this.check()) { // let probe timeout return; } var uri = this.uri(); var protocols = this.protocols; var opts = {}; if (this.extraHeaders) { opts.headers = this.extraHeaders; } if (this.localAddress) { opts.localAddress = this.localAddress; } try { this.ws = new WebSocketImpl(uri, protocols, opts); } catch (err) { return this.emit('error', err); } };
์ ๋ฐ์ ์ผ๋ก,
this.isReactNative
๋ฅผ ํตํด ReactNative์์ ์ฌ์ฉํ ๋ ํ์ํ์ง ์์ ์ถ๊ฐopts
๋ฅผ ์ ์ธํ๊ธฐ ์ํด EngineIO์ ๋ํ pull ์์ฒญ์ ์์ฑํ๋ฉด ํด๋น ์์ผ์์ SocketIO ํด๋ผ์ด์ธํธ๋ฅผ ์ฌ์ฉํ๋ ๋ชจ๋ ์ฌ์ฉ์์ ๋ํด์ด ๊ฒฝ๊ณ ๋ฅผ ์ต์ ํ ์ ์์ต๋๋ค. ReactNative ํ๋ก์ ํธ.๊ทํ์ ์ฝ๋๋ก ์ธํด ์๋ฒ์ ์ฐ๊ฒฐํ ์ ์์ผ๋ฉฐ EngineIO์ ์๋ ๋ ๋์์ด ๋ณ๊ฒฝ๋์์ผ๋ฏ๋ก ํด๋น ์ต์ ์ if ๋ฌธ์ผ๋ก ๋ํํ์ง๋ง ์ ์ํ ๊ฒ๊ณผ ๋์ผํ ํจ์น ๋ฉ์ปค๋์ฆ์ ์ฌ์ฉํ์ต๋๋ค.
// patches/EngineIOHeaderWarning.js const WS = require('engine.io-client/lib/transports/websocket'); var WebSocketImpl = WebSocket; // eslint-disable-line no-undef WS.prototype.doOpen = function() { if (!this.check()) { // let probe timeout return; } var uri = this.uri(); var protocols = this.protocols; var opts = {}; if(!this.isReactNative){ opts.agent = this.agent; opts.perMessageDeflate = this.perMessageDeflate; // SSL options for Node.js client opts.pfx = this.pfx; opts.key = this.key; opts.passphrase = this.passphrase; opts.cert = this.cert; opts.ca = this.ca; opts.ciphers = this.ciphers; opts.rejectUnauthorized = this.rejectUnauthorized; } if (this.extraHeaders) { opts.headers = this.extraHeaders; } if (this.localAddress) { opts.localAddress = this.localAddress; } try { this.ws = this.usingBrowserWebSocket && !this.isReactNative ? protocols ? new WebSocketImpl(uri, protocols) : new WebSocketImpl(uri) : new WebSocketImpl(uri, protocols, opts); } catch (err) { return this.emit('error', err); } if (this.ws.binaryType === undefined) { this.supportsBinary = false; } if (this.ws.supports && this.ws.supports.binary) { this.supportsBinary = true; this.ws.binaryType = 'nodebuffer'; } else { this.ws.binaryType = 'arraybuffer'; } this.addEventListeners(); };
const _io = ioClient.connect(socketurl, { forceNode: true });
์ผ์ด์ผ
์ ์ ํจ๊ป ์๋ํฉ๋๋ค. ๊ฐ์ฌํฉ๋๋ค!
"socket.io-client": "^2.3.0"
const socket = io(host, { forceNode: true });
const _io = ioClient.connect(socketurl, { forceNode: true });
์ผ์ด์ผ์ ์ ํจ๊ป ์๋ํฉ๋๋ค. ๊ฐ์ฌํฉ๋๋ค!
"socket.io-client": "^2.3.0"
const socket = io(host, { forceNode: true });
๊ทธ๊ฒ์ ๋์๊ฒ๋ ํจ๊ณผ์ ์ด์์ต๋๋ค! ํฐ ๊ฐ์ฌ๋ฅผ ๋๋ฆฐ๋ค
ํค๋๋ฅผ ์ถ๊ฐํ๊ธฐ ๋งํ๋ฉด ์๋ํฉ๋๋ค!
socket = io.connect('SOCKET_URL_HERE', {
jsonp: false,
agent: '-',
pfx: '-',
cert: '-',
ca: '-',
ciphers: '-',
rejectUnauthorized: '-',
perMessageDeflate: '-'
});
const _io = ioClient.connect (socketurl, {forceNode : true});
์ด๊ฒ์ ๊ฒฝ๊ณ ๋ฅผ ํด๊ฒฐํ์ง๋ง ๋ด ์์ผ ์ฐ๊ฒฐ์ ์ ๋ง ๋๋ฆฌ๊ฒ (์ค์ ๋ก ์๊ฐ ์ด๊ณผ)ํ๋ฏ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค.
const _io = ioClient.connect (socketurl, {forceNode : true});
์ด๊ฒ์ ๊ฒฝ๊ณ ๋ฅผ ํด๊ฒฐํ์ง๋ง ๋ด ์์ผ ์ฐ๊ฒฐ์ ์ ๋ง ๋๋ฆฌ๊ฒ (์ค์ ๋ก ์๊ฐ ์ด๊ณผ)ํ๋ฏ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ฌ๊ธฐ์๋ ๋์ผ : ์ฐ๊ฒฐ ์๊ฐ ์ด๊ณผ!
froceNode๋ฅผ ์ ๊ฑฐํ๊ณ 2.1.0
๋ค์ด ๊ทธ๋ ์ด๋ํ๋๋ฐ ์ ๋๋ก ์๋ํ์ต๋๋ค. ๋ํ ์๋ฎฌ๋ ์ดํฐ ์ฅ์น์ IP๊ฐ ์๋ localhost๋ฅผ ์ฌ์ฉํ๋ ๋ฌธ์ ๊ฐ์์์ต๋๋ค.
๋์ผํ @SaveYourTime , ์ ๋ "socket.io-client": "^2.3.0"
์ฌ๊ธฐ https://socket.io/docs/client-api/๋ฅผ ์ฐธ์กฐํ์ญ์์ค
forceNode
์ต์
์ ์ถ๊ฐํ๋ ์ด์
์ด ๋ฌธ์ ๋ engine.io-client
v3.4.3์์ ์์ ๋์ด์ผํ๋ฏ๋ก yarn.lock
ํ์ผ๊ณผ node_modules
๋๋ ํฐ๋ฆฌ๋ฅผ ์ญ์ ํ ๋ค์ yarn install
๋ฅผ ์คํํ๊ธฐ ๋งํ๋ฉด๋ฉ๋๋ค.
ref https://github.com/socketio/engine.io-client/commit/2f5c948abe8fd1c0fdb010e88f96bd933a3792ea
@BassemN ์๋ง๋ yarn.lock ํ์ผ์ ์ญ์ ํ๋ ๋์ yarn upgrade-interactive
๋ฅผ ์ฌ์ฉํ์ฌ ๋ฒ์ ์ ์์ ํ๊ฒ ๋ฒํ ํ ์ ์์ต๋๊น?
ํจํค์ง๋ฅผ ์ ๊ทธ๋ ์ด๋ ํ ๋๋ง๋ค ํ์ผ์ ์์ ํ ํ๊ดดํ๋ฉด "์ ๊ธ"ํ์ผ์ด๋ผ๋ ๊ฐ๋ ์ ๋ค์ ๋ฐ ์ง๊ด์ ์ผ๋ก ๋ณด์ ๋๋ค. ๐
https://github.com/socketio/engine.io-client/commit/e5bc1063cc90a7b6262146c7b5338ffff1ff9e5b ( [email protected]
๋ฐ [email protected]
)๋ก ์์ ํด์ผํฉ๋๋ค.
forceNode
์ต์
๊ณผ ๊ด๋ จํ์ฌ React Native๋ ์ค์ ๋ก WebSocket ( ref )์ ์ง์ํ๋ฏ๋ก forceNode: true
๊ฐ ํ์ํ์ง ์์ต๋๋ค. ๊ทธ๋ ์ง ์์ต๋๊น?
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋๋ฅผ ์ํด 2.1.1๋ก ๋ค์ด ๊ทธ๋ ์ด๋ํ์ ๋ ์๋ํ์ต๋๋ค.
yarn add [email protected]
์ฒ์์๋ ํจํค์ง์ ๋ค๋ฅธ ๋ถ ๋ฒ์ ์ ํ์ฉํ๋
socket.io-client@^2.1.1
๋ฒ์ ์์^
๊ธฐํธ๋ฅผ ์ถ๊ฐํ๊ธฐ ๋๋ฌธ์ ์๋ํ์ง ์์์ต๋๋ค.