-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Missing full working examples #13
Comments
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 😆 This is the only output I'm getting, when running the above code. I might have misunderstood something 😄 |
👋 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('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('data', (data) => console.log(`${prefix} Received: ${data}`));
stream.on('error', (err) => console.error(prefix, err));
stream.write('"Hello world" from ' + prefix);
}
}); When
|
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! |
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
The text was updated successfully, but these errors were encountered: