diff --git a/packages/server/play-node-go/src/App.js b/packages/server/play-node-go/src/App.js index 05cb505..b9da5a7 100644 --- a/packages/server/play-node-go/src/App.js +++ b/packages/server/play-node-go/src/App.js @@ -7,9 +7,6 @@ import { initState } from './reducers/init/stateReducer.init'; import indexServices from './services/api/indexServices'; import './App.scss'; -import socketIOClient from 'socket.io-client'; -const socket = socketIOClient(config.socketAddress); - function App() { const [ state, dispatch ] = useReducer( @@ -35,8 +32,8 @@ function App() { }, []) const socketConnect = () => { - if (state.connect) return; - dispatch({type:'SOCKET', message: 'LAUNCH', body:{socket, dispatch}}); + if (state.connect.type) return; + dispatch({type:'SOCKET', message: 'LAUNCH', body:{nsp:'', dispatch}}); } useEffect(() => { @@ -77,7 +74,7 @@ function App() {
{state.connect ? '✓' : ' ⃠'}
+{state.connect.type ? '✓' : ' ⃠'}
); diff --git a/packages/server/play-node-go/src/io.js b/packages/server/play-node-go/src/io.js index 4219a30..cdf0c7f 100644 --- a/packages/server/play-node-go/src/io.js +++ b/packages/server/play-node-go/src/io.js @@ -1,25 +1,26 @@ import socketIOClient from 'socket.io-client'; import config from './config'; - -const launch = (socket, dispatch) => { +const launch = (nsp, dispatch) => { + const socket = socketIOClient(`${config.socketAddress}/${nsp}`); socket.on('connected', () => { - dispatch({type:'SOCKET', message:'CONNECTED', body:{}}); + dispatch({ type:'SOCKET', message:'CONNECTED', body:{nsp: socket.nsp} }); }); socket.on('connect_error', err => { - dispatch({type: 'ERR', message:'SOCKET_ERROR', body: {socketError: err}}); + dispatch({ type: 'ERR', message:'SOCKET_ERROR', body: { socketError: err }}); }); socket.on('error', err => { - dispatch({type: 'ERR', message:'SOCKET_ERROR', body: {socketError: err}}); + dispatch({ type: 'ERR', message:'SOCKET_ERROR', body: { socketError: err } }); }); socket.on('room_connected', (data) => { - dispatch({type: 'ROOM', message: 'CONNECT_ROOM', body: data}); + dispatch({ type: 'ROOM', message: 'CONNECT_ROOM', body: data }); }); socket.on('new_user', (data) => { - console.log(data) + console.log('new_user received') + dispatch({ type: 'ROOM', message: 'NEW_USER', body: data }) }) return socket; diff --git a/packages/server/play-node-go/src/pages/Room/Room.js b/packages/server/play-node-go/src/pages/Room/Room.js index fb9d938..d808365 100644 --- a/packages/server/play-node-go/src/pages/Room/Room.js +++ b/packages/server/play-node-go/src/pages/Room/Room.js @@ -13,7 +13,7 @@ import Development from '../../components/Display/Development/Development'; const Room = (props) => { const { state, dispatch} = props; const roomId = parseInt(useParams().id) || 0; - const socket = socketIOClient(`${config.socketAddress}/${roomId}`); + // const socket = socketIOClient(`${config.socketAddress}/${roomId}`); const fetchRoomAPI = async () => { const response = await roomsServices.getRoomService(roomId); @@ -37,7 +37,7 @@ const Room = (props) => { const action = { type: 'SOCKET', message: 'CONNECT_ROOM', - body: {user: state.user, room: roomId, socket} + body: {user: state.user, room: roomId, dispatch} } dispatch(action) } @@ -81,7 +81,7 @@ const Room = (props) => {