1

I'm using electron-vue build an APP. I need create a tcp connection, and I use net.Socket().But i get a no response when I set HOST. I need use the socket global, so I create a class like this:

import crc16ccitt from 'crc/crc16ccitt';
const net = require('net');

class TcpClient {

    tcp = null;

    alive = false;

    connect(options) {
        return new Promise((resolve, reject) => {

        this.tcp = new net.Socket();
        this.tcp.connect(options, () => {
            this.alive = true;
            resolve();
            console.log('connect server');
        });

        this.tcp.on('close', () => {
            this.alive = false;
            console.log('close');
            reject();
        });
        this.tcp.on('error', () => {
            console.log('error');
        });
    });
  }
}

export default TcpClient;

and then I put it in the main.js like this:

Vue.prototype.$tcp = new TcpClient();

but when I use in vue instance like this: this.$tcp.connect({ port: 8000, host: 127.0.0.1 }); Nothing happend, no errors, no result, but when I reload my page, I think connect a moment, and my server shows: events.js:174 throw er; // Unhandled 'error' event ^

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
Emitted 'error' event at:
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)

And if I don't set HOST, just port, it works well.

starhu
  • 11
  • 1

0 Answers0