TooTallNate / node-proxy-agent

Maps proxy protocols to `http.Agent` implementations
285 stars 69 forks source link

How can i use ws with HttpsProxyAgent #41

Closed syter closed 1 year ago

syter commented 5 years ago

My project is create a websocket connection between my computer and a internet websocket server. Now I want to use proxy. I get some proxy IP from Internet. Such as: 'http://51.38.71.101:8080'.

Here is my code:

var WebSocket = require('ws');
var http = require("http");
var HttpProxyAgent = require('http-proxy-agent');
var HttpsProxyAgent = require('https-proxy-agent');
var urlUtil = require('url');

var reconnectInterval = 30 * 1000;
var reconnectTimeCount = 0;
var reconnectTotalCount = 10000;
var socket;
var proxy = 'http://51.38.71.101:8080';
var options = urlUtil.parse(proxy);
// var agent = new HttpsProxyAgent(options);
var agent = new HttpProxyAgent(options);

var connect = function(){
    try {
        console.log("========000000======");
        var url = 'ws://115.231.140.70';
        // socket = new WebSocket(url, { agent: agent });
        socket = new WebSocket(url);
        console.log("========1111======");
        socket.on('open', function() {
            console.log("=====connect====success====");
        });
        socket.on('message', function (data, flags) {
            console.log("=====received msg====");
        });
        socket.on('error', function(err) {
            console.log("************" + 'socket error'+ require('util').inspect(err));
        });
        socket.on('close', function() {
            console.log('socket close' + "===============reconnectTimeCount="+reconnectTimeCount);
            if (reconnectTimeCount <= reconnectTotalCount) {
                setTimeout(connect, reconnectInterval);
                reconnectTimeCount ++;
            }
        });
        console.log("========222222======");
    } catch (e) {
        console.log("========333333======");
    }   

};

connect();

When I use socket = new WebSocket(url);, my console log is:

SyterLius-MacBook-Air:app Syter$ node test.js 
========000000======
========1111======
========222222======
=====connect====success====

But when I use socket = new WebSocket(url, { agent: agent });, my console log is:

SyterLius-MacBook-Air:app Syter$ node test.js 
========000000======
========1111======
========222222======
************socket errorError: Unexpected server response: 101
    at ClientRequest.req.on (/Users/Syter/Documents/workcode/moshan/hyserver/node_modules/ws/lib/websocket.js:579:7)
    at ClientRequest.emit (events.js:188:13)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:562:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:113:17)
    at Socket.socketOnData (_http_client.js:449:20)
    at Socket.emit (events.js:188:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:145:17)
socket close===============reconnectTimeCount=0

I have test proxy IP in http.get way like documents said, it's no problem So how can I do for it??

And my node version is v11.7.0 ws version is 7.0.1 https-proxy-agent version is 2.2.1 http-proxy-agent version is 2.1.0

TooTallNate commented 1 year ago

This code in this repository has been moved to the proxy-agents monorepo, so I am closing this pull request. If you feel that this issue still exists as of the latest release, feel free to open a new issue over there.