react native socket always not connected socket.conneted shows false always

Viewed 149
"""
import { StatusBar } from 'expo-status-bar';
import React, { useEffect , useRef} from 'react';
import { StyleSheet, Text, View , Button} from 'react-native';
window.navigator.userAgent = 'react-native'
import io from 'socket.io-client/dist/socket.io';

export default function App(){
  
  const socketRef = useRef()
 
  useEffect(
        () => {
            socketRef.current = io.connect("http://localhost:4000")
            console.log("cococo", socketRef.current.connected)
            return () => socketRef.current.disconnect()
        },
        [  ]
    )
 
  const hi = () =>  {
    console.log("hhiii", socketRef.current.connected)
        // socketRef.current.emit("message", { name, message })
        socketRef.current.emit("chat", "sage")
  };
  


  return (
    <View style={styles.container}>
      <Text>Open up App.js to  working on your app!</Text>
      <Button title="press" onPress={()=>{hi()}} />
      <Button title="bye" onPress={()=>{onMessageSubmit()}} />
      <StatusBar style="auto" />
    </View>
  );
}
"""

react native trying to connect to my node server sending a message from client emit message but always the socket is not connected can not send message to the server socket emit("chat", "sage") socket.connected is always shows false cant figure this out

there server side code is

const app = require('express')()
const http = require('http').createServer(app)
const io = require('socket.io')(http)

io.on('connection', socket => {
  // socket.on('d', ({ name, message }) => {
  //   io.emit('d', { name, message })
  // })
  socket.on("chat", msg =>{
    console.log(msg)
  })
})




http.listen(4000, function() {
  console.log('listening on port 4000')
})
0 Answers
Related