1.0.0 • Published 8 years ago
co-fiber v1.0.0
Co-fiber
Co-fiber is a service with co-flow and fiber yielding, with on socket message injection. .
Install to your repo:
$ npm install co-fiber --save
Version
0.0.1
Tech
Co-fiber uses a number of open source projects to work properly:
- co
- node fibers
- koa-compose
Example
var coFiber = require('./lib/co-fiber.js');
var coFiber = require('./lib/co-fiber.js');
var middleware_ex = function *(next){
var ctx = this;
ctx.a = ctx.a ? ctx.a : 0;
ctx.b = ctx.b ? ctx.b : 1;
ctx.c = ctx.c ? ctx.c : 2;
ctx.d = ctx.d ? ctx.d : 3;
ctx.count = ctx.count ? ctx.count : 0;
ctx.msg = 'starting';
console.log(ctx.msg);
yield next;
};
var middleware_ex2 = function *(next){
console.log('Hello there!');
yield next;
};
var middleware_ex3 = function *(next){
console.log('Hello there 2!');
var ctx = this;
console.log('ctx.a: ' + ctx.a);
console.log('ctx.b: ' + ctx.b);
console.log('ctx.c: ' + ctx.c);
console.log('ctx.d: ' + ctx.d);
yield next;
};
var middleware_ex4 = function *(){
var ctx = this;
ctx.on('data', function(data){
if(data.toString() === 'Hello'){
console.log('message: ' + data);
console.log('wait... ' + new Date);
ctx.a += 1;
setTimeout(function(){
console.log('test');
ctx.b += 1;
console.log('ctx.b: ' + ctx.b);
}, 2000);
ctx.b += 1;
console.log('ctx.a: ' + ctx.a);
console.log('ctx.b: ' + ctx.b);
console.log('ok... ' + new Date);
}
});
return true;
};
var complete = function *(){
console.log('finished');
return true;
};
var app = coFiber();
app.use(middleware_ex);
app.use(middleware_ex2);
app.fiber(middleware_ex4, complete);
app.use(middleware_ex3);
app.start(4444);
// Fired off by curl 127.0.0.1:4444
Example Client
var net = require('net');
var client = new net.Socket();
client.connect(4444, '127.0.0.1', function() {
console.log('Connected');
client.write('Hello');
});
Development
Want to contribute? Great!
License
MIT
Authors