We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hi there. I am doing my application as given in the example, but for some reason, no information comes to the server connection
Client.js
import TcpSocket from 'react-native-tcp-socket'; import NetInfo from '@react-native-community/netinfo'; export const client = new TcpSocket.Socket(); export const init = async port => { const ip = await getWifi(); const options = { port: port, host: `127.0.0.1`, localAddress: `127.0.0.1`, reuseAddress: true, localPort: port, // interface: "wifi", }; client.connect(options, () => { client.write(`connected to server`); }); client.on('data', data => { console.log(`new data`, data.toString()); }); };
Server.js
import TcpSocket from 'react-native-tcp-socket'; import NetInfo from '@react-native-community/netinfo'; export const server = new TcpSocket.Server(); export let address = null; export const closeServer = () => { if (server) { console.log(`server kapandi`); server.close(); } else { console.log('server yok'); } }; export const init = async () => { const ip = await getWifi(); server.on('connection', socket => { socket.on('data', () => { socket.write('Echo server\r\n'); }); }); server.listen({port: 0, host: `127.0.0.1`, reuseAddress: true}, () => { const port = server.address()?.port; if (!port) throw new Error('Server port not found'); address = server.address(); }); };
SocketProvider.js
import React, {useEffect, useState} from 'react'; import {SocketContext} from './SocketContext'; import {closeServer, server, init} from '../server/Server'; export default function SocketProvider({children}) { const [isInitServer, setIsInitServer] = useState(false); useEffect(() => { if (isInitServer) { console.log(`say hi`); server.on('connection', socket => { console.log(socket.address()); socket.on('data', data => { console.log(`server method `, data.toString()); }); }); init(); } return () => { if (isInitServer) { closeServer(); setIsInitServer(false); } }; }, [isInitServer]); return ( <SocketContext.Provider value={{ isInitServer, setIsInitServer, }}> {children} </SocketContext.Provider> ); }
ClientProvider.js
import React, {useEffect, useState} from 'react'; import {ClientContext} from './ClientContext'; import {init, client} from '../server/Client'; export default function ClientProvider({children}) { const [isJoinedClient, setIsJoinedClient] = useState({ isConnectected: false, port: null, }); useEffect(() => { if (isJoinedClient.isConnectected && isJoinedClient.port !== null) { console.log(`ok`); client.on('data', data => { console.log(`client method`, data.toString()); }); client.on('error', err => { console.log(`client error: `, err); client.destroy(); beginIsJoined(); }); init(isJoinedClient.port); } return () => { if (isJoinedClient.isConnectected && isJoinedClient.port !== null) { client.destroy(); beginIsJoined(); } }; }, [isJoinedClient]); const handleIsJoined = (boolVal, port) => { setIsJoinedClient(prev => ({ ...prev, isConnectected: boolVal, port: port, })); }; const beginIsJoined = () => { setIsJoinedClient(prev => ({ ...prev, isConnectected: false, port: null, })); }; return ( <ClientContext.Provider value={{isJoinedClient, setIsJoinedClient: handleIsJoined}}> {children} </ClientContext.Provider> ); }
server side SocketProvider is not logging the socket address for some reason can you help me.
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Hi there. I am doing my application as given in the example, but for some reason, no information comes to the server connection
Client.js
Server.js
SocketProvider.js
ClientProvider.js
server side SocketProvider is not logging the socket address for some reason can you help me.
The text was updated successfully, but these errors were encountered: