javascript 如何在浏览器上运行 node.js 客户端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28059521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to run node.js client on browser
提问by Kawin Netvichitpan
everyone
每个人
I'm very new to node.js. I'm trying to do a tcp server <-> client using node.js. So far so good. The server script can be run Ok. Also the client script can be run OK.
我对 node.js 很陌生。我正在尝试使用 node.js 做一个 tcp 服务器 <-> 客户端。到现在为止还挺好。服务器脚本可以正常运行。客户端脚本也可以正常运行。
But the problem is I could only get the client to run from the terminal by typing command (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.
但问题是我只能通过输入命令(node client.js)让客户端从终端运行。
问题是我想在浏览器中运行它,以便我可以从浏览器上的服务器显示接收数据。
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.
谢谢你。
回答by Paul
Node.js is not browser 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 (e.g. socket.io, which offers server and browser clients).
Node.js 不是浏览器 javascript。它的许多部分使用了浏览器上下文中不可用的操作系统功能。在客户端停留在浏览器中的同时做您想做的事情的方法是不使用 TCP 套接字,而是查看 WebSockets(例如 socket.io,它提供服务器和浏览器客户端)。
回答by dayuoba
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' 从浏览器端获取数据