大家好
我对node.js非常陌生,现在想使用node.js制作一个tcp服务器和客户端。目前进展顺利,服务器脚本可以正常运行,客户端脚本也可以正常运行。
但是问题是我只能通过在终端中输入命令(node client.js)来运行客户端。
我想在浏览器中运行它,这样就可以将从服务器接收到的数据显示在浏览器上了。
请问我该怎么做呢?
求助。
Kawin.
以下是客户端代码。(我记不得最初创建这个脚本的人是谁了。我从某个地方复制并粘贴了它,但忘记了书签链接。很抱歉没有给这个脚本的所有者留下信用。)
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');
});
谢谢你。
感谢您。