簡體   English   中英

如何使用 Node.JS 通過純文本協議發送消息

[英]How to Send Messages over the Plaintext Protocol using Node.JS

我對 Sockets 和 NodeJS 還是有點陌生​​,我希望能夠使用 NodeJS 通過純文本協議與 RoboMaster 機器人通信,而不是像文檔中解釋的那樣使用 Python。 我不確定如何使用 NodeJS 執行此操作,如果我的應用程序套接字是客戶端或服務器,我會有點困惑。 我希望將文檔中的示例代碼轉換為 NodeJS 友好版本,但不確定如何。 我已經研究過像 Socket.io 這樣的東西,但我不確定這是否是我需要使用的。

任何幫助,將不勝感激。

編輯:我找到了這個例子,它看起來和我需要的很相似,但我不確定。

事實證明,我可以使用 net 模塊與 RoboMaster 機器人進行通信。 使用此處的代碼如下所示:

var net = require('net');

var HOST = '192.168.2.1';
var PORT = 40923;

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('command;');

});

// 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');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM