是否可以使用同一端口启用tcp,http和websocket? [英] Is it possible to enable tcp, http and websocket all using the same port?

查看:2126
本文介绍了是否可以使用同一端口启用tcp,http和websocket?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试启用 tcp websocket.io 在同一端口上进行通信.我开始使用tcp服务器(在////行上方),它可以正常工作.然后,我运行了在websocket.io上找到的 echo服务器示例(////行下面的部分),它也起作用.但是当我尝试将它们合并在一起时,tcp不再起作用.

I am trying to enable tcp, http and websocket.io communication on the same port. I started out with the tcp server (part above //// line), it worked. Then I ran the echo server example found on websocket.io (part below //// line), it also worked. But when I try to merge them together, tcp doesn't work anymore.

SO,是否可以使用同一端口启用tcp,http和websockets?还是我必须在另一个端口上监听tcp连接?

SO, is it possible to enable tcp, http and websockets all using the same port? Or do I have to listen on another port for tcp connections?

var net = require('net');
var http = require('http');
var wsio = require('websocket.io');

var conn = [];

var server = net.createServer(function(client) {//'connection' listener
    var info = {
        remote : client.remoteAddress + ':' + client.remotePort
    };
    var i = conn.push(info) - 1;
    console.log('[conn] ' + conn[i].remote);

    client.on('end', function() {
        console.log('[disc] ' + conn[i].remote);
    });

    client.on('data', function(msg) {
        console.log('[data] ' + conn[i].remote + ' ' + msg.toString());
    });

    client.write('hello\r\n');
});

server.listen(8080);

///////////////////////////////////////////////////////////

var hs = http.createServer(function(req, res) {
    res.writeHead(200, {
        'Content-Type' : 'text/html'
    });
    res.end(['<script>', "var ws = new WebSocket('ws://127.0.0.1:8080');", 'ws.onmessage = function (data) { ws.send(data); };', '</script>'].join(''));
});

hs.listen(server);

var ws = wsio.attach(hs);
var i = 0, last;

ws.on('connection', function(client) {

    var id = ++i, last

    console.log('Client %d connected', id);

    function ping() {
        client.send('ping!');
        if (last)
            console.log('Latency for client %d: %d ', id, Date.now() - last);
        last = Date.now();
    };

    ping();
    client.on('message', ping);

});

推荐答案

同一端口可以处理多个不同的协议,但有一些警告:

You can have multiple different protocols handled by the same port but there are some caveats:

  • 服务器必须采用某种方法来检测(或协商)客户端希望使用的协议.您可以将单独的端口视为检测客户端希望使用的协议的常规方法.

  • There must be some way for the server to detect (or negotiate) the protocol that the client wishes to speak. You can think of separate ports as the normal way of detecting the protocol the client wishes to speak.

只有一个服务器进程可以实际在端口上侦听.该服务器可能仅用于检测协议类型,然后转发到其他多个服务器,但每个端口均由单个服务器进程拥有.

Only one server process can be actually listening on the port. This server might only serve the purpose of detecting the type of protocol and then forwarding to multiple other servers, but each port is owned by a single server process.

您不能在服务器首先讲话的情况下支持多种协议(因为无法检测到客户端的协议).您可以支持具有多个客户端优先协议的单个服务器优先协议(通过在接受之后添加一小段延迟以查看客户端是否将发送数据),但这有点不可思议.

You can't support multiple protocols where the server speaks first (because there is no way to detect the protocol of the client). You can support a single server-first protocol with multiple client-first protocols (by adding a short delay after accept to see if the client will send data), but that's a bit wonky.

WebSocket协议的一个明确的设计目标是允许WebSocket和HTTP协议共享同一服务器端口.最初的WebSocket握手是与HTTP兼容的升级请求.

An explicit design goal of the WebSocket protocol was to allow WebSocket and HTTP protocols to share the same server port. The initial WebSocket handshake is an HTTP compatible upgrade request.

websockify 服务器/桥是服务器的示例,该服务器可以在服务器上使用5种不同的协议相同的端口:HTTP,HTTPS(加密的HTTP),WS(WebSockets),WSS(加密的WebSockets)和Flash策略响应.服务器偷看传入请求的第一个字符,以确定它是否经过TLS加密(HTTPS或WSS)或是否以<"开头. (Flash策略请求).如果这是Flash策略请求,则它将读取该请求,响应并关闭连接.否则,它将读取HTTP握手(加密或不加密),并且Connection和Upgrade标头确定它是WebSocket请求还是纯HTTP请求.

The websockify server/bridge is an example of a server that can speak 5 different protocols on the same port: HTTP, HTTPS (encrypted HTTP), WS (WebSockets), WSS (encrypted WebSockets), and Flash policy response. The server peeks at the first character of the incoming request to determine if it is TLS encrypted (HTTPS, or WSS) or whether it begins with "<" (Flash policy request). If it is a Flash policy request, then it reads the request, responds and closes the connection. Otherwise, it reads the HTTP handshake (either encrypted or not) and the Connection and Upgrade headers determine whether it is a WebSocket request or a plain HTTP request.

免责声明:我制作了websockify

这篇关于是否可以使用同一端口启用tcp,http和websocket?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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