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

Missing full working examples #13

Closed
ehanoc opened this issue Dec 28, 2022 · 3 comments
Closed

Missing full working examples #13

ehanoc opened this issue Dec 28, 2022 · 3 comments

Comments

@ehanoc
Copy link

ehanoc commented Dec 28, 2022

Would like to explore this lib for a specific use case of tpc protocol multiplexing ; but there's no working example or fully e2e tests can show how to use it. Could this be added ?

Thanks

@firecow
Copy link

firecow commented Jan 2, 2023

import net from "net";
import {Server, Client} from "yamux-js";

// tcp server
const tunnelServer = net.createServer();
tunnelServer.on("connection", function(sck) {
    console.log("tcp client connected", sck.remotePort);
    const yamuxServer = new Server(function(muxStream) {
        muxStream.on("data", data => console.log("server muxstream data", `${data}`, data));
        muxStream.on("end", () => {
            muxStream.end("Yes i'm here, thanks for contacting me, sweet client !!");
        });
        muxStream.on("error", err => console.error("server muxstream error", err));
    });
    yamuxServer.pipe(sck).pipe(yamuxServer);
});
tunnelServer.listen(3500);

// tcp client
const socket = net.createConnection(3500);
socket.on("connect", () => {
    const yamuxClient = new Client();
    yamuxClient.pipe(socket).pipe(yamuxClient);

    const muxStream = yamuxClient.open();
    muxStream.on("end", () => console.log("client muxstream end"));
    muxStream.on("data", (data) => console.log("client", `${data}`, data));
    muxStream.on("error", err => console.error("client", err));
    muxStream.end("Hello server, are you there?");
});

Here is one, except it's not working 😆

image

This is the only output I'm getting, when running the above code.

I might have misunderstood something 😄

@th-ch
Copy link
Owner

th-ch commented Jan 31, 2023

👋 Hey folks, indeed there was a bug that could show up with some examples like this one! With the bug fixed, a simple example looks like that:

// server.js
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('error', (err) => console.error('Server stream error', err));
        },
        {
            enableKeepAlive: false,
        }
    );
    socket.pipe(yamuxServer).pipe(socket);
});
server.listen(3500);
// client.js
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('data', (data) => console.log(`${prefix} Received: ${data}`));
        stream.on('error', (err) => console.error(prefix, err));

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

When node server.js is running, launching node client.js, the following output should appear:

server.js client.js
Connected on port 59878 Connected on port 59878
Received: "Hello world" from [Stream 1] [Stream 1] Received: Server acknowledged: "Hello world" from [Stream 1]
Received: "Hello world" from [Stream 3] [Stream 3] Received: Server acknowledged: "Hello world" from [Stream 3]
Received: "Hello world" from [Stream 5] [Stream 5] Received: Server acknowledged: "Hello world" from [Stream 5]

@th-ch
Copy link
Owner

th-ch commented Feb 1, 2023

The fix has been merged and released (v0.1.2) and the example added to the readme! Closing the issue, feel free to re-open if needed!

@th-ch th-ch closed this as completed Feb 1, 2023
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

3 participants