带有 JavaScript TCP 客户端的浏览器 [英] Browser with JavaScript TCP Client

查看:45
本文介绍了带有 JavaScript TCP 客户端的浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 node.js 中构建了一个 tcp 服务器,它接收和发送数据到每个连接到这个服务器的客户端(c++ 中的客户端).现在我想让带有 javascript 的浏览器连接到这个服务器,但我在服务器的控制台中收到的只是这个:

I have built a tcp server within node.js which receives and sends data to each client connecting to this server (clients in c++). Now I want the browser with javascript to connect to this server, but all I receive in the console of the server is this:

127.0.0.1:55680 message: GET / HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/
24.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Sec-WebSocket-Version: 13
Origin: null
Sec-WebSocket-Key: 19YxKKtCqZeuxtOSlaoswg==
Connection: keep-alive, Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket

我在节点中的服务器看起来像这样(来自互联网的例子):

my server in node looks like this (example from the internet):

// 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");

我的 javascript 浏览器客户端:

my browser client in javascript:

$(document).ready(function() 
{
    function connect()
    {
        // Websocket
        var socket = new WebSocket("ws://127.0.0.1:80");

        // Nach dem öffnen des Sockets den Status anzeigen
        socket.onopen   = function()    { message('Socket Status: '+socket.readyState+' (open)');   }
        // Nach dem empfangen einer Nachricht soll diese angezeigt werden
        socket.onmessage= function(msg) { message('Empfangen: '+msg.data);                          }           

        // SENDEN
        function send()
        {
            var text = $('#text').val();    // Text aus textbox in Variable schreiben 
            socket.send(text);      // Den Text aus der Variable an den Socket senden
            message('Gesendet : '+text) // Anzeigen was gesendet wurde
            $('#text').val("");     // Text-Inhalt der Textbox löschen
        }

        // Funktion welche die Nchrichten an das Log anfügt
        function message(msg)
        {
            $('#Log').append(msg+'</br>');
        }

        // Enter taste auf der TextBox zum senden behandeln
        $('#text').keypress(function(event) 
        {
            if (event.keyCode == '13') 
            {
                send();
            }
        }); 
    }

    connect();

});

我粘贴的第一条消息是我的浏览器连接到 tcp 服务器时在服务器中收到的消息.我不知道如何向服务器发送数据或写入消息.除此之外我不知道为什么会显示这样的消息.

The first message I pasted is the message I get in the server when my browser connects to the tcp server. I don't know how I can send data or write messages to the server. In addition to that I don't know why there is displayed such a message.

推荐答案

问题是您在 NodeJS 中仅使用纯 TCP 连接.如果您想通过 WebSockets 进行通信,您还必须在服务器端 (NodeJS) 上使用一些 WS 模块.看看 https://github.com/einaros/ws

The problem is that you are using just pure TCP connection in NodeJS. If you want to communicate via WebSockets you have to use some WS module on the server side (NodeJS) as well. Have a look at https://github.com/einaros/ws

这篇关于带有 JavaScript TCP 客户端的浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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