connect socket

This commit is contained in:
Sorrel Bri 2020-01-07 23:18:08 -08:00 committed by sorrelbri
parent 395e2b1d86
commit 397617eb34
2 changed files with 15 additions and 6 deletions

View file

@ -1,14 +1,21 @@
import React from 'react'; import React, {useState} from 'react';
import './App.scss'; import './App.scss';
import config from './config'; import config from './config';
import socketIOClient from 'socket.io-client';
export const socket = socketIOClient(config.apiAddress);
function App() { function App() {
let fetchData = {}; const [fetchData, setFetchData] = useState();
fetch(config.apiAddress).then(res => {console.log(res);fetchData = res.body}).then(() => console.log(fetchData)) const [socketData, setSocketData] = useState();
fetch(config.apiAddress).then(res => res.text()).then(data => setFetchData(data));
socket.emit('connect');
socket.on('connected', data => setSocketData(data.message));
return ( return (
<div className="App"> <div className="App">
<h1>React Boilerplate</h1> <h1>React Boilerplate</h1>
{fetchData ? <p></p> : <></>} {fetchData ? <p>{fetchData}</p> : <></>}
{socketData ? <p>{socketData}</p> : <></>}
</div> </div>
); );
} }

View file

@ -1,9 +1,11 @@
const production = { const production = {
apiAddress: null apiAddress: null,
endpoint: null
} }
const development = { const development = {
apiAddress: 'http://localhost:8000' apiAddress: 'http://localhost:8000',
endpoint: 'http://localhost:3000'
} }
const config = process.env.REACT_APP_ENVIRONMENT === 'production' const config = process.env.REACT_APP_ENVIRONMENT === 'production'