Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodejs http #17

Open
ynchuan opened this issue Mar 3, 2019 · 0 comments
Open

nodejs http #17

ynchuan opened this issue Mar 3, 2019 · 0 comments

Comments

@ynchuan
Copy link
Owner

ynchuan commented Mar 3, 2019

var http = require('http');
var server = http.createServer();
var log = function (str) {
    console.log('##' + Date.now() + '##');
    console.log(str);
}
server.on('request', function (req, res) {
    // req读入流 res写出流
    req.on('data', function (chunk) {
        log('server-request-data-' + chunk.toString());
    }).on('end', function () {
        log('server-request-end');
        res.writeContinue();
        res.end();
    });
}).on('connection', function (socket) {
    // socket 双工流
    socket.on('data', function (chunk) {
        log('server-connection-data-' + chunk.toString());
    });
    socket.on('end', function () {
        log('server-connection-end');
    });
    // socket.write('server socket');
    // socket.end('server socket end');
}).listen(9999, function () {
    var opt = {
        protocol: 'http:',
        hostname: '127.0.0.1',
        port: 9999,
        method: 'POST',
    };
    var client = http.request(opt);
    client.on('response', function (res) {
        // res 可读流
        res.on('data', function (chunk) {
            log('client-response-data-' + chunk.toString());
        });
        res.on('end', function () {
            log('client-response-end');
        });
    }).on('socket', function (socket) {
        // socket duplux流,可读可写
        socket.on('data', function (chunk) {
            log('client-socket-data-' + chunk.toString());
        });
        socket.on('end', function () {
            log('client-socket-end');
        });
        // socket.write('client socket');
        // socket.end('client socket end');
    });
    client.write('client write');
    client.end('client write end');
});

##1551610895924##
server-connection-data-POST / HTTP/1.1
Host: 127.0.0.1:9999
Connection: close
Transfer-Encoding: chunked

c
client write
10
client write end
0


##1551610895925##
server-request-data-client write
##1551610895926##
server-request-data-client write end
##1551610895926##
server-request-end
##1551610895931##
client-socket-data-HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Date: Sun, 03 Mar 2019 11:01:35 GMT
Connection: close
Content-Length: 0


##1551610895932##
client-response-end

读写流备注

  • 写出流拥有写出功能,可以执行write,例如客户端client实例,服务端response
  • 读入流拥有读权限,可以执行数据接收功能,例如客户端response,服务端request
  • client通过socket事件可以获取数据通信客户端socket
    = server端通过connection事件可以获取数据通信服务端socket,
  • 两个socket是duplux流,可写可读

connect代理触发

  • client端method=connect建立隧道
  • server端on('connect',function(req,socket,head){}),method=connect请求直接接入

upgrade触发

headers: {
   'Connection': 'Upgrade',
   'Upgrade': 'websocket'
}

continue触发

headers: {
    'Expect': '100-continue'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant