當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js readStream.setRawMode()用法及代碼示例

readStream.setRawMode()方法是‘tty’模塊內的ReadStream類的內置應用程序編程接口,用於設置Read stream對象的原始模式以用作原始設備。

用法:

const readStream.setRawMode( mode )

參數:此方法將布爾值作為參數。

返回值:此方法不返回任何值。

範例1: 文件名:index.js



Javascript

// Node.js program to demonstrate the 
// readStream.setRawMode() method 
  
// Importing dgram module 
var dgram = require('dgram'); 
  
  
// Creating and initializing client 
// and server socket 
var client = dgram.createSocket("udp4"); 
var server = dgram.createSocket("udp4"); 
  
// Cathing the message event 
server.on("message", function (msg) { 
  
    // Creating and intializing a  
    // ReadStream object 
    let ReadStream = process.stdin; 
  
    // Setting the read stream Raw mode 
    // by using setRawMode() method 
    ReadStream.setRawMode(false); 
  
    // Checking if it is configured or not  
    // by using isRaw() method 
    const status = ReadStream.isRaw; 
  
    // Displaying the result 
    if (status) 
        process.stdout.write(msg + "configured" + "\n"); 
    else
        process.stdout.write(msg + "not configured" + "\n"); 
  
    // Exiting process 
    process.exit(); 
}) 
  
    // Binding server with port 
    .bind(1234, () => { 
    }); 
  
// Client sending message to server 
client.send("It is ", 0, 7, 1234, "localhost");

輸出:

It is not configured

範例2: 文件名:index.js

Javascript

// Node.js program to demonstrate the 
// readStream.setRawMode() method 
  
// Creating and initializing a ReadStream object 
let ReadStream = process.stdin; 
  
// Setting the read stream Raw mode 
// by using setRawMode() method 
ReadStream.setRawMode(true); 
  
// Checking if it is configured or not  
// by using isRaw() method 
const status = ReadStream.isRaw; 
  
// Displaying the result 
if(status) 
    console.log("It is configured"); 
else
    console.log("It is not configured");

輸出:

It is configured

使用以下命令運行index.js文件:

node index.js

參考:https://nodejs.org/dist/latest-v12.x/docs/api/tty.html#tty_readstream_setrawmode_mode

相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js readStream.setRawMode() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。