Cha*_*nap 5 network-programming node.js
我需要建立到 smtp 服务器的 tcp 套接字连接。是否可以通过nodejs上的代理服务器进行连接?是否有任何可用的 npm 模块?我根本找不到。
var net = require('net');
var HOST = '127.0.0.1';
var PORT = 6969;
var client = new net.Socket();
client.connect(PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
client.write('I am here!');
});
// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on('data', function(data) {
console.log('DATA: ' + data);
});
// Add a 'close' event handler for the client socket
client.on('close', function() {
console.log('Connection closed');
});
Run Code Online (Sandbox Code Playgroud)
小智 -1
是的,可以使用以下 NPM 模块之一:
http-proxy-agent:HTTP 端点的 HTTP(s) 代理 http.Agent 实现
https-proxy-agent:HTTPS 端点的 HTTP(s) 代理 http.Agent 实现
pac-proxy-agent:用于 HTTP 和 HTTPS 的 PAC 文件代理 http.Agent 实现
ocks-proxy-agent:HTTP 和 HTTPS 的 SOCKS (v4a) 代理 http.Agent 实现
HTTPS 代理示例:
var url = require('url');
var WebSocket = require('ws');
var HttpsProxyAgent = require('https-proxy-agent');
// HTTP/HTTPS proxy to connect to
var proxy = process.env.http_proxy || 'http://168.63.76.32:3128';
console.log('using proxy server %j', proxy);
// WebSocket endpoint for the proxy to connect to
var endpoint = process.argv[2] || 'ws://echo.websocket.org';
var parsed = url.parse(endpoint);
console.log('attempting to connect to WebSocket %j', endpoint);
// create an instance of the `HttpsProxyAgent` class with the proxy server information
var opts = url.parse(proxy);
var agent = new HttpsProxyAgent(opts);
// finally, initiate the WebSocket connection
var socket = new WebSocket(endpoint, { agent: agent });
socket.on('open', function () {
console.log('"open" event!');
socket.send('hello world');
});
socket.on('message', function (data, flags) {
console.log('"message" event! %j %j', data, flags);
socket.close();
});
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助。
| 归档时间: |
|
| 查看次数: |
6343 次 |
| 最近记录: |