Last active
February 8, 2019 00:47
-
-
Save morkai/a6e8d696f11aa2fb903a369c775e8c76 to your computer and use it in GitHub Desktop.
Readable stream state (1=true 0=false n=null)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const net = require('net'); | |
const READABLE = 1; | |
const client = new net.Socket(); | |
const oldRead = client.read; | |
let connected = false; | |
client.on('error', (err) => log('error')); | |
client.on('close', () => | |
{ | |
log(`close`); | |
if (connected) | |
{ | |
//console.log(client); | |
} | |
connected = false; | |
setTimeout(connect, 333); | |
}); | |
client.on('connect', () => | |
{ | |
log(`connect`); | |
connected = true; | |
}); | |
if (READABLE) | |
client.on('readable', () => | |
{ | |
log(`readable`, client.read()); | |
}); | |
else | |
client.on('data', (data) => | |
{ | |
log(`data`, data); | |
}); | |
connect(); | |
setInterval(() => | |
{ | |
if (connected) | |
{ | |
log(`write`); | |
client.write(Buffer.from([0, 1, 2])); | |
} | |
}, 1000); | |
function connect() | |
{ | |
log(`connecting`); | |
client.connect(502, '127.0.0.1'); | |
} | |
function log(event, data) | |
{ | |
console.log( | |
`${event.padEnd(10)}: ` | |
+ `readable=${f(client.readable)} ` | |
+ `isPaused()=${f(client.isPaused())} ` | |
+ `reading=${f(client._readableState.reading)} ` | |
+ `flowing=${f(client._readableState.flowing)} ` | |
+ `data=`, data); | |
} | |
function f(v) | |
{ | |
return v === true ? 1 : v === false ? 0 : v === null ? 'n' : v; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
λ node10.15 client.js | |
connecting: readable=0 isPaused()=1 reading=0 flowing=0 data= undefined | |
connect : readable=1 isPaused()=1 reading=1 flowing=0 data= undefined | |
write : readable=1 isPaused()=1 reading=1 flowing=0 data= undefined | |
readable : readable=1 isPaused()=1 reading=1 flowing=0 data= <Buffer 00 01 02> | |
write : readable=1 isPaused()=1 reading=1 flowing=0 data= undefined | |
readable : readable=1 isPaused()=1 reading=1 flowing=0 data= <Buffer 00 01 02> | |
write : readable=1 isPaused()=1 reading=1 flowing=0 data= undefined | |
readable : readable=1 isPaused()=1 reading=1 flowing=0 data= <Buffer 00 01 02> | |
write : readable=1 isPaused()=1 reading=1 flowing=0 data= undefined | |
readable : readable=1 isPaused()=1 reading=1 flowing=0 data= <Buffer 00 01 02> | |
write : readable=1 isPaused()=1 reading=1 flowing=0 data= undefined | |
readable : readable=1 isPaused()=1 reading=1 flowing=0 data= <Buffer 00 01 02> | |
write : readable=1 isPaused()=1 reading=1 flowing=0 data= undefined | |
readable : readable=1 isPaused()=1 reading=1 flowing=0 data= <Buffer 00 01 02> | |
error : readable=0 isPaused()=1 reading=1 flowing=0 data= undefined | |
close : readable=0 isPaused()=1 reading=1 flowing=0 data= undefined | |
connecting: readable=0 isPaused()=1 reading=1 flowing=0 data= undefined | |
error : readable=0 isPaused()=1 reading=0 flowing=0 data= undefined | |
close : readable=0 isPaused()=1 reading=0 flowing=0 data= undefined | |
connecting: readable=0 isPaused()=1 reading=0 flowing=0 data= undefined | |
error : readable=0 isPaused()=1 reading=0 flowing=0 data= undefined | |
close : readable=0 isPaused()=1 reading=0 flowing=0 data= undefined | |
connecting: readable=0 isPaused()=1 reading=0 flowing=0 data= undefined | |
error : readable=0 isPaused()=1 reading=0 flowing=0 data= undefined | |
close : readable=0 isPaused()=1 reading=0 flowing=0 data= undefined | |
connecting: readable=0 isPaused()=1 reading=0 flowing=0 data= undefined | |
error : readable=0 isPaused()=1 reading=0 flowing=0 data= undefined | |
close : readable=0 isPaused()=1 reading=0 flowing=0 data= undefined | |
connecting: readable=0 isPaused()=1 reading=0 flowing=0 data= undefined | |
connect : readable=1 isPaused()=1 reading=0 flowing=0 data= undefined | |
write : readable=1 isPaused()=1 reading=0 flowing=0 data= undefined | |
write : readable=1 isPaused()=1 reading=0 flowing=0 data= undefined | |
write : readable=1 isPaused()=1 reading=0 flowing=0 data= undefined | |
write : readable=1 isPaused()=1 reading=0 flowing=0 data= undefined | |
write : readable=1 isPaused()=1 reading=0 flowing=0 data= undefined | |
write : readable=1 isPaused()=1 reading=0 flowing=0 data= undefined | |
write : readable=1 isPaused()=1 reading=0 flowing=0 data= undefined | |
write : readable=1 isPaused()=1 reading=0 flowing=0 data= undefined | |
write : readable=1 isPaused()=1 reading=0 flowing=0 data= undefined | |
^C | |
λ node10.9 client.js | |
connecting: readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
connect : readable=1 isPaused()=0 reading=1 flowing=n data= undefined | |
write : readable=1 isPaused()=0 reading=1 flowing=n data= undefined | |
readable : readable=1 isPaused()=0 reading=1 flowing=n data= <Buffer 00 01 02> | |
write : readable=1 isPaused()=0 reading=1 flowing=n data= undefined | |
readable : readable=1 isPaused()=0 reading=1 flowing=n data= <Buffer 00 01 02> | |
write : readable=1 isPaused()=0 reading=1 flowing=n data= undefined | |
readable : readable=1 isPaused()=0 reading=1 flowing=n data= <Buffer 00 01 02> | |
write : readable=1 isPaused()=0 reading=1 flowing=n data= undefined | |
readable : readable=1 isPaused()=0 reading=1 flowing=n data= <Buffer 00 01 02> | |
error : readable=0 isPaused()=0 reading=1 flowing=n data= undefined | |
close : readable=0 isPaused()=0 reading=1 flowing=n data= undefined | |
connecting: readable=0 isPaused()=0 reading=1 flowing=n data= undefined | |
error : readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
close : readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
connecting: readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
error : readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
close : readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
connecting: readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
error : readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
close : readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
connecting: readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
error : readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
close : readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
connecting: readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
connect : readable=1 isPaused()=0 reading=0 flowing=n data= undefined | |
write : readable=1 isPaused()=0 reading=1 flowing=n data= undefined | |
readable : readable=1 isPaused()=0 reading=1 flowing=n data= <Buffer 00 01 02> | |
write : readable=1 isPaused()=0 reading=1 flowing=n data= undefined | |
readable : readable=1 isPaused()=0 reading=1 flowing=n data= <Buffer 00 01 02> | |
write : readable=1 isPaused()=0 reading=1 flowing=n data= undefined | |
readable : readable=1 isPaused()=0 reading=1 flowing=n data= <Buffer 00 01 02> | |
write : readable=1 isPaused()=0 reading=1 flowing=n data= undefined | |
readable : readable=1 isPaused()=0 reading=1 flowing=n data= <Buffer 00 01 02> | |
write : readable=1 isPaused()=0 reading=1 flowing=n data= undefined | |
readable : readable=1 isPaused()=0 reading=1 flowing=n data= <Buffer 00 01 02> | |
write : readable=1 isPaused()=0 reading=1 flowing=n data= undefined | |
readable : readable=1 isPaused()=0 reading=1 flowing=n data= <Buffer 00 01 02> | |
write : readable=1 isPaused()=0 reading=1 flowing=n data= undefined | |
readable : readable=1 isPaused()=0 reading=1 flowing=n data= <Buffer 00 01 02> | |
write : readable=1 isPaused()=0 reading=1 flowing=n data= undefined | |
readable : readable=1 isPaused()=0 reading=1 flowing=n data= <Buffer 00 01 02> | |
write : readable=1 isPaused()=0 reading=1 flowing=n data= undefined | |
readable : readable=1 isPaused()=0 reading=1 flowing=n data= <Buffer 00 01 02> | |
error : readable=0 isPaused()=0 reading=1 flowing=n data= undefined | |
close : readable=0 isPaused()=0 reading=1 flowing=n data= undefined | |
connecting: readable=0 isPaused()=0 reading=1 flowing=n data= undefined | |
error : readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
close : readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
connecting: readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
error : readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
close : readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
connecting: readable=0 isPaused()=0 reading=0 flowing=n data= undefined | |
^C |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const net = require('net'); | |
const server = net.createServer(); | |
server.on('connection', (conn) => | |
{ | |
console.log('server#connection'); | |
conn.on('error', (err) => console.error('conn#error', err.message)); | |
conn.on('close', (err) => console.log('conn#close')); | |
conn.on('readable', () => | |
{ | |
const data = conn.read(); | |
if (data) | |
{ | |
console.log('conn#readable', data); | |
conn.write(data); | |
} | |
}); | |
}); | |
server.on('error', (err) => console.error(err.message)); | |
server.on('close', () => console.log('server#close')); | |
server.on('listening', () => console.log('server#listening')); | |
server.listen(502); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment