在对Node.js服务器发出HTTP请求/响应后保持套接字打开 [英] Keeping socket open after HTTP request/response to Node.js server

查看:83
本文介绍了在对Node.js服务器发出HTTP请求/响应后保持套接字打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了支持基于HTTP的协议(Icecast源协议),我需要能够在HTTP请求完成后使用Node.js的http.Server中的套接字。示例请求如下所示:

To support a protocol (Icecast Source Protocol) based on HTTP, I need to be able to use a socket from Node.js's http.Server once the HTTP request is finished. A sample request looks like this:

Client->Server: GET / HTTP/1.0
Client->Server: Some-Headers:header_value
Client->Server: 
Server->Client: HTTP/1.0 200 OK
Server->Client: 
Client->Server: <insert stream of binary data here>

这是为了支持互联网广播流的来源,流数据的来源是客户端在这种情况下。

This is to support the source of an internet radio stream, the source of the stream data being the client in this case.

我有什么方法可以使用内置的http.Server中的Node.js吗?我试过这个:

Is there any way I can use Node.js's built in http.Server? I have tried this:

this.server = http.createServer(function (req, res) {
    console.log('connection!');
    res.writeHead(200, {test: 'woot!'});
    res.write('test');
    res.write('test2');
    req.connection.on('data', function (data) {
        console.log(data);
    });
}).listen(1337, '127.0.0.1');

如果我telnet到1337端口并提出请求,我可以看到前几个字符我在服务器控制台窗口上键入的内容,然后服务器关闭连接。理想情况下,我会无限期地保持该套接字打开,并在初始请求完成后将HTTP部分从循环中取出。

If I telnet into port 1337 and make a request, I am able to see the first couple characters of what I type on the server console window, but then the server closes the connection. Ideally, I'd keep that socket open indefinitely, and take the HTTP part out of the loop once the initial request is made.

这是否可以使用股票http。服务器类?

Is this possible with the stock http.Server class?

推荐答案

我解决这个问题的方法是重新发明轮子并编写自己的HTTP-ish服务器。不完美,但它的工作原理。希望其中一些Node.js类的内部有一天会暴露出来。

My solution to this problem was to reinvent the wheel and write my own HTTP-ish server. Not perfect, but it works. Hopefully the innards of some of these stock Node.js classes will be exposed some day.

这篇关于在对Node.js服务器发出HTTP请求/响应后保持套接字打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆