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

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

问题描述

我正在尝试启用 tcphttpwebsocket.io同一个端口上的通信.我开始使用 tcp 服务器(位于////行上方的部分),它工作正常.然后我运行了 在 websocket.io 上找到的回声服务器示例(////行下方的部分),它也有效.但是当我尝试将它们合并在一起时,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
');
});

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 策略请求,则它会读取请求、响应并关闭连接.否则,它会读取 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天全站免登陆