Rapsssito / react-native-tcp-socket

React Native TCP socket API for Android, iOS & macOS with SSL/TLS support.
MIT License
304 stars 80 forks source link

Connect to HTTP / telnet server #140

Closed a613 closed 2 years ago

a613 commented 2 years ago

Description

Client does not work.

Steps to reproduce

npx react-native init fresh --template react-native-template-typescript

yarn add react-native-tcp-socket

Add this to index.js:

import TcpSocket from 'react-native-tcp-socket';
console.log('connecting...');
const client = TcpSocket.createConnection(
  {host: 'google.com', port: 80},
  () => {
    console.log('connected');
    client.write('GET /');
  },
);
client.on('data', d => console.log(d));

Run the app, see the warning and no logging of any inbound message from the server.

WARN  socketDidDisconnect with nil clientDelegate for 0

Current behavior

Upon init, the client logs a warning and does not communicate with the server.

 LOG  connecting...
 WARN  socketDidDisconnect with nil clientDelegate for 0
 LOG  Running "fresh" with {"rootTag":51,"initialProps":{}}
 LOG  connected

Expected behavior

Upon init, the client does what the docs say. It should behave the same as telnet google.com 80 does when sent an initial line of GET /.

Screenshots

N/A

Relevant information

OS macOS 12.2.1
react-native 0.67.2
react-native-tcp-socket 5.5.0
Rapsssito commented 2 years ago

@a613, could you test if the example code works for your setup?

a613 commented 2 years ago

Yes but it does not work if I change it to talk to a real server like google or my API server. Then it immediately fails with socketDidDisconnect with nil clientDelegate for 0.

function init() {
    // server.on('connection', (socket) => {
    //     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');
        client.connect(
            {
                port: 80,
                host: 'google.com',
                // localAddress: '127.0.0.1',
                reuseAddress: true,
                // localPort: 20000,
                // interface: "wifi",
                // tls: true
            },
            () => {
                client.write('GET /');
            }
        );
    // });
Rapsssito commented 2 years ago

@a613, you might want to take a look at #95. However, to sum up, it is an issue on your side: you must append CR + LF (\r\n) to the message you are sending.