Thursday 4 May 2017

Emergency! Error: This socket has been ended by the other party

I revised appium source code, add my code, when i connect to the port that is forwarded to device and send command to port, it comes out:

Error: This socket has been ended by the other party

and my code is like this:

return await new Promise((resolve, reject) => {
            try {
                this.socketClient = net.connect(this.webSocket);
                // Windows: the socket errors out when ADB restarts. Let's catch it to avoid crashing.
                this.socketClient.on('error', (err) => {
                    if (!this.ignoreUnexpectedShutdown) {
                        //throw new Error(`Android bootstrap socket crashed: ${err}`);
                        log.debug('//////////////////////////////////')
                        log.debug(err)
                        log.debug('//////////////////////////////////')
                        throw new Error(`Android testbundle socket crashed: ${err}`)
                    }
                });
                this.socketClient.once('connect', () => {
                    log.info("Android bundle socket is now connected");
                    resolve();
                });
            } catch (err) {
                reject(err);
            }
        })

after that, I use this.socketClient to send command like this:

async sendCommand(type, extra = {}) {
        if (!this.socketClient) {
            log.debug('==========socket closed========')
            throw new Error('Socket connection closed unexpectedly');
        }
        return await new B((resolve, reject) => {
            let cmd = Object.assign({cmd: type}, extra);
            let cmdJson = `${JSON.stringify(cmd)}\n`;
            log.debug(`Sending command to android testbundle: ${_.trunc(cmdJson, 1000).trim()}`);

            this.socketClient.write(cmdJson);
            this.socketClient.setEncoding('utf8');

            let streamData = '';
            this.socketClient.on('data', (data) => {
                try {
                    streamData = JSON.parse(streamData + data);
                    // we successfully parsed JSON so we've got all the data,
                    // remove the socket listener and evaluate
                    this.socketClient.removeAllListeners('data');
                    if (streamData.status === 0) {
                        resolve(streamData.value);
                    }
                    log.debug("Received command result from bundle:" + JSON.stringify(streamData));
                    reject(errorFromCode(streamData.status));
                } catch (ign) {
                    log.debug("Stream still not complete, waiting");
                    streamData += data;
                }
            })
        })

    }

But, I always get the error:

[debug] [bundle] //////////////////////////////////
[debug] [bundle] Error: This socket has been ended by the other party
    at Socket.writeAfterFIN [as write] (net.js:291:12)
    at ..\../lib/bundle.js:160:31

Anyone can help me...



via TreeCatCat

No comments:

Post a Comment