GithubHelp home page GithubHelp logo

Comments (5)

Rapsssito avatar Rapsssito commented on May 23, 2024 1

I hope this example can help you!

const connectedSockets = []; // We store the connected sockets here
// Create the server
const server = TcpSocket.createServer((socket) => { 
  connectedSockets.push(socket); // Add the new connected socket
});

// Send a  message to the first connected client
connectedSockets[0].write('Hello server');

from react-native-tcp-socket.

Rapsssito avatar Rapsssito commented on May 23, 2024

Following the documentation:

var server = TcpSocket.createServer((socket) => { // <--- This is the socket connected to the client
  // You may want to store the `socket` in an Array or any other data structure based on your needs
  // in order to access a specific one
  socket.on('data', (data) => {
    socket.write('Echo server', data);
  });

  socket.on('error', (error) => {
    console.log('An error ocurred with client socket ', error);
  });

  socket.on('close', (error) => {
    console.log('Closed connection with ', socket.address());
  });
}).listen(12345, '0.0.0.0');

server.on('error', (error) => {
  console.log('An error ocurred with the server', error);
});

server.on('close', () => {
  console.log('Server closed connection');
});

from react-native-tcp-socket.

aymensmurf avatar aymensmurf commented on May 23, 2024

So to my understanding you have to make a new socket for every client. am I understanding that right?

from react-native-tcp-socket.

Rapsssito avatar Rapsssito commented on May 23, 2024

@aymensmurf, the socket is created automatically for every client. You just access it from the createServer() callback.

from react-native-tcp-socket.

aymensmurf avatar aymensmurf commented on May 23, 2024

It still a little vague, but I will have to look more into it. Thanks for your help.

from react-native-tcp-socket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.