Skip to content

Wrap other transports to have a reliable packet stream

License

Notifications You must be signed in to change notification settings

mrtcode/opstream

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpStream - wrap other transports to have a reliable packet stream

  • Guarantees that packets are delivered in the right sequence
  • Guarantees that each packet is delivered only one time
  • Buffers packets if connection is lost
  • Resends packets when connection is restored
  • Pauses sending new packets if other endpoint is busy and don't acknowledge them

opstream.push() => opstream.send => socket.io(client) => socket.io(server) => opstream.recv => opstream.pop()

Install

Browser

<script src="opstream.js">

Node.js or Browserify

npm install opstream
var OpStream = require('opstream');

Send and receive packets

//A simple example how OpStreams are connected
var OpStream = require('./opstream');

var server = new OpStream;
var client = new OpStream;

// connect server and client op streams
server.send = function (data) {
  setTimeout(function () {
    client.recv(data)
  }, 0);
};

client.send = function (data) {
  setTimeout(function () {
    server.recv(data)
  }, 0);
};

//imitate client side
function clientThread() {
  for (var i = 0; i < 100; i++) {
    client.push('packet' + i);
  }
}

//imitate server side
function serverThread() {
  server.onReadable = function () {
    var op;
    while (op = server.pop()) {
      console.log('server.pop()', op);
    }
  };
}

serverThread();
clientThread();

Examples

OpStream + Socket.io example

About

Wrap other transports to have a reliable packet stream

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published