简体   繁体   English

如何在浏览器上运行 node.js 客户端

[英]how to run node.js client on browser

everyone每个人

I'm very new to node.js.我对 node.js 很陌生。 I'm trying to do a tcp server <-> client using node.js.我正在尝试使用 node.js 做一个 tcp 服务器 <-> 客户端。 So far so good.到目前为止,一切都很好。 The server script can be run Ok.服务器脚本可以运行了。 Also the client script can be run OK.客户端脚本也可以正常运行。

But the problem is I could only get the client to run from the terminal by typing command (node client.js).但问题是我只能通过键入命令(node client.js)让客户端从终端运行。
The thing is I would like to run it in a browser so I could take the data received from server display on browser.问题是我想在浏览器中运行它,这样我就可以把从服务器接收到的数据显示在浏览器上。

How do I do that?我怎么做?

Please help.请帮忙。

Kawin.卡文。

This is the client code.这是客户端代码。 (I can't remember who originally created this script. I copy and paste it from somewhere but forget to bookmark from which I get the link. Sorry for not putting the credit to the owner of this script.) (我不记得最初是谁创建了这个脚本。我从某个地方复制并粘贴了它,但忘记了我从中获取链接的书签。很抱歉没有把功劳归功于这个脚本的所有者。)

var net = require('net');

var HOST = '192.168.0.88';
var PORT = 8888;

var client = new net.Socket();
client.connect(PORT, HOST, function() {

    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
    // Write a message to the socket as soon as the client is connected, the   server will receive it as message from the client 
    client.write('B2\r\n');
});

// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on('data', function(data) {
    console.log('DATA: ' + data);
    // Close the client socket completely
    client.destroy();
});

// Add a 'close' event handler for the client socket
client.on('close', function() {
    console.log('Connection closed');
}); 

Thank you.谢谢你。

Node.js is not browser javascript. Node.js 不是浏览器 javascript。 There are many parts of it that use OS features not available in a browser context.它的许多部分使用浏览器上下文中不可用的操作系统功能。 The way to do what you're looking to do while staying in the browser for the client, is to not use a TCP socket, but instead look into WebSockets (eg socket.io, which offers server and browser clients).在为客户端停留在浏览器中的同时做您想做的事情的方法是不要使用 TCP 套接字,而是查看 WebSockets(例如 socket.io,它提供服务器和浏览器客户端)。

Times are changing.时代在变。 It's just been announced that it might be possible to use node.js in the browser soon.刚刚宣布可能很快在浏览器中使用 node.js。 Check out this link: Run Node.js in the browser查看此链接:在浏览器中运行 Node.js

The thing is I would like to run it in a browser so I could take the data received from server display on browser.问题是我想在浏览器中运行它,这样我就可以把从服务器接收到的数据显示在浏览器上。

I think you need 'http' module.我认为你需要'http'模块。

var http=require('http');
var server=http.Server(function(req,res) {
    res.end('<p>hello world</p><script>alert("hello world")</script>');
});

server.listen(8080);

so you can get data from browser side by typing URL 'localhost:8080'因此您可以通过键入 URL 'localhost:8080' 从浏览器端获取数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM