everyone如何在瀏覽器上運行node.js客戶端
我對node.js非常陌生。 我試圖做一個tcp服務器< - >客戶端使用node.js.到現在爲止還挺好。 服務器腳本可以運行正常。 此外,客戶端腳本可以運行正常。
但問題是我只能通過鍵入命令(node client.js)讓客戶端從終端運行。
事情是我想在瀏覽器中運行它,所以我可以從瀏覽器上的服務器顯示接收數據。
我該怎麼做?
請幫忙。卡爾文:
卡溫。
這是客戶端代碼。 (我不記得是誰最初創建這個腳本。我複製並從某處粘貼,但忘了書籤從中我得到的鏈接。對不起,不把信用卡給該腳本的擁有者。)
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');
});
謝謝。
*「我該怎麼辦呢?」 *你不能。 Node.js和瀏覽器是兩種不同的環境。如果您使用某個環境特有的功能(例如TCP套接字),則不能在其他環境中使用相同的腳本。 – 2015-01-21 04:20:36
謝謝。所以我想我可能不得不改變我的計劃來實現這一目標。 – 2015-01-21 06:32:07