shovon / node-rudp

Reliable UDP for Node.js
33 stars 14 forks source link

Server #12

Open amiiigh opened 5 years ago

amiiigh commented 5 years ago

Hey, Would you please also provide an example for the Server part?

How can I get the data after the server received any? shouldn't be a listener on connection and emit the data after?

shovon commented 5 years ago

HI @amiiigh. I'll be on it. I'll document on how to use this library.

In the meanwhile, this is how you use the library for the purposes of using it like a server.

var rudp = require('rudp');
var dgram = require('dgram');

var socket = dgram.createSocket('udp4');

// Where `localPort` is the port to connect to.
socket.bind(localPort);

var server = new rudp.Server(socket);

server.on('connection', socket => {
  socket.on('data', data => {
    console.log(data.toString('utf8'));
  });
  socket.write('Hello, World!');
});

Essentially, it is used like Node.js' net library, when creating a server.

amiiigh commented 5 years ago

Hey @shovon Thanks for the example

I'm just a bit confused now. Correct me if I'm wrong, so the 'Server' and 'Connection' are an EventEmitter but not a Stream. I can't find write function for Connection in its file. Eventually, I want to pipe the connection data to another stream to handle uplink and downlink limit on my application so I think I need them to be a Stream, am I right?

Also can the server reply to the client by just using that connection, with something like connection.send() or connection.write() ?

My last question is about net.socket.end() function in net library in node. Is there anything equivalent to that in this library?

shovon commented 5 years ago

Correct me if I'm wrong, so the 'Server' and 'Connection' are an EventEmitter but not a Stream. I can't find write function for Connection in its file.

You are right. It is not a stream. I will need to implement the stream functionality.

Also can the server reply to the client by just using that connection, with something like connection.send() or connection.write() ?

Yes. With connection.send.

My last question is about net.socket.end() function in net library in node. Is there anything equivalent to that in this library?

This library is a wrapper for the dgram library. So just call close on the initial UDP socket that is being wrapped.

amiiigh commented 5 years ago

Hey, @shovon it's me again :D I'm trying to make the connection class like net.socket so my understanding is I need to make connection a Duplex stream, am I right? For doing that I just need to implement a couple of functions like _write and _read _final.

Do I need to modify any other part of the code?

for the _write function it's just going to call the send function, right?

Also, do you have any plan to make your library for es6? I can help if you like to do that.

shovon commented 5 years ago

I am more than thankful that you want to look into helping me turn Connection into Node.js' stream.Duplex, as well as translate the code into ES6.

So here are the steps that we want to do in order to achieve the above.

  1. convert Connection into a stream.Duplex
  2. translate the code into ES6

Stretch goal: maintain TypeScript types.

shovon commented 5 years ago

@amiiigh let me know if you are still interested in doing so.

shovon commented 5 years ago

Please pull from master. I've made some changes, recently.

amiiigh commented 5 years ago

@amiiigh let me know if you are still interested in doing so.

yeah man, let's do it!

Please pull from master. I've made some changes, recently. okay!