Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Substream cannot trigger ‘close’ or ‘end’ events. #17

Open
jiawei14755 opened this issue Oct 12, 2024 · 0 comments
Open

Substream cannot trigger ‘close’ or ‘end’ events. #17

jiawei14755 opened this issue Oct 12, 2024 · 0 comments

Comments

@jiawei14755
Copy link

client

const net = require('net');
const { Client } = require('yamux-js');

const nbStreams = 3;
const socket = net.createConnection(3500, () => {
    console.log('Connected on port', socket.localPort);
    const yamuxClient = new Client();
    socket.pipe(yamuxClient).pipe(socket);

    for (let i = 0; i < nbStreams; i++) {
        const stream = yamuxClient.open();

        const prefix = `[Stream ${stream.ID()}]`;
        stream.on('end', () => console.log(`${prefix} Stream end`));
        stream.on('close', () => console.log(`${prefix} Stream close`));
        stream.on('error', (err) => console.error(prefix, err));

        stream.on('data', (data) => {
            console.log(`${prefix} Received: ${data}`);
            stream.close();
            stream.end();
        });

        stream.write('"Hello world" from ' + prefix);
    }
});

server

const net = require('net');
const { Server } = require('yamux-js');
const server = net.createServer((socket) => {
    console.log('Connected on port', socket.remotePort);
    const yamuxServer = new Server(
        (stream) => {
            stream.on('data', (data) => {
                console.log(`Received: ${data}`);
                stream.write('Server acknowledged: ' + data);
            });
            stream.on('end', () => {
                console.log('Ending stream');
            });
            stream.on('close', () => {
                console.log('closed stream');
            });
            stream.on('error', (err) => console.error('Server stream error', err));
        },
        {
            enableKeepAlive: false,
        }
    );
    socket.pipe(yamuxServer).pipe(socket);
});
server.listen(3500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant