Node.js websocket-server和tcp-server连接 [英] Node.js websocket-server and tcp-server connection

查看:184
本文介绍了Node.js websocket-server和tcp-server连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与此问题相关使用JavaScript TCP Client的浏览器我问我是否可以连接从浏览器到tcp服务器。我发现它不会工作,所以我问另一个解决方案。 '0101'提供我建立两个服务器。一个tcp服务器用于连接到一个c ++应用程序,一个websockets服务器从浏览器接收数据。我最初构建了它们中的每一个,但是我不知道如何连接它们,所以我可以从c ++应用程序中的浏览器接收数据。

Related to this question Browser with JavaScript TCP Client I asked whether I can connect from a browser to a tcp server. I found out that it won't work so I asked for another solution. '0101' provided me to built up two servers. One tcp server for a c++ application that connects to and one websockets server that receives data from the browser. I have originally built up each one of them, but I don't know how to connect them so I can receive data from the browser in the c++ application.

这里是websockets-server:

Here is the websockets-server:

var ClientListe = {};
// Anzahl der Verbundenen Clients
var ClientAnzahl=0;

// Websocket-Server
var WebSocketServer = require('ws').Server
var wss = new WebSocketServer({host: '127.0.0.1',port: 80});

wss.on('connection', function(ws) 
{
    // Client-Anzahl hochzählen
    ClientAnzahl++;
    // Client-Verbindung mit in die Client-Liste Aufnehmen
    ws['AUTH'] = ClientAnzahl;
    ClientListe[ws['AUTH']] = ws;
    // Ausgabe
    console.log('client '+ClientAnzahl+' verbunden...');

    ws.on('message', function(message) 
    {
        console.log('von Client empfangen: ' + message);

        for(client in ClientListe)
        {
            ClientListe[client].send('von Server empfangen: ' + message);
        }

    });

    ws.on('close', function() 
    {
        // Client aus der ClientListe Löschen
        delete ClientListe[ws['AUTH']];

        // Nachricht der Trennung an die Console ausgeben
        console.log('Client '+ ws['AUTH'] +' getrennt.');
    });

});

,这里是tcp服务器:

and here is the tcp server:

// Load the TCP Library
net = require('net');

// Keep track of the chat clients
var clients = [];

// Start a TCP Server
net.createServer(function (socket) {

// Identify this client
socket.name = socket.remoteAddress + ":" + socket.remotePort;

// Put this new client in the list
clients.push(socket);

// Send a nice welcome message and announce
socket.write("Welcome " + socket.name + "\n");
broadcast(socket.name + " joined the server\n", socket);

// Handle incoming messages from clients.
socket.on('data', function (data) {
broadcast(socket.name + " message: " + data, socket);
});

// Remove the client from the list when it leaves
socket.on('end', function () {
clients.splice(clients.indexOf(socket), 1);
broadcast(socket.name + " left the server.\n");
});
// Send a message to all clients
function broadcast(message, sender) {
clients.forEach(function (client) {
// Don't want to send it to sender
if (client === sender) return;
client.write(message);
});
// Log it to the server output too
process.stdout.write(message)
}

}).listen(80);

// Put a friendly message on the terminal of the server.
console.log("TCP Server running at localhost port 80\n");

这两个都是从互联网复制的,用于测试某些情况

Both are copied out of the internet for testing some cases

推荐答案

创建TCP服务器(NodeJS示例)

Create a TCP server (NodeJS example)

var net = require("net");

var server = net.createServer(function(c) { //'connection' listener
    console.log('server connected');

    c.on('end', function() {
        console.log('server disconnected');
    });

    c.write('hello\r\n');
    c.pipe(c);
});

server.listen(8124, function() { //'listening' listener
    console.log('server bound');
});

然后在同一个文件中(当然可以)创建一个不同端口号的WS服务器

Then in the same file (optionally of course) create a WS server with different port number

var WebSocketServer = require("ws").Server;

var wss = new WebSocketServer({
    port: 8080
});

wss.on("connection", function(ws) {
    console.log("CONNECTED");

    // ws.on("message"), ws.on("close"), ws.on("error")
});

现在您应该有两个服务器,一个用于常规套接字,另一个用于WebSockets。

Now you should have two servers, one for regular sockets and another one for WebSockets.

//正如我在前面的问题和Pete中提到的,在C ++中使用WebSockets也好,而不是创建两个服务器...

// As I mentioned in the previous question and Pete as well, it is a lot better to use WebSockets in C++ as well instead of creating two servers...

这篇关于Node.js websocket-server和tcp-server连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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