koajs / koa.io

[MAINTAINERS WANTED] Realtime web framework combine koa and socket.io.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

router.js bug -for socket.io Sending and getting data (acknowledgements)

tosscaster opened this issue · comments

Router.prototype.middleware = function () {
  var self = this;
  var gen = compose(this.fns);
  return function* router(next) {
    debug('in router middleware');
    if (!self.fns.length) {
      debug('router not exist');
      return yield* next;
    }
    var context = this;
    var socket = this.socket;

    // replace socket.onevent to start the router
    socket._onevent = socket.onevent;
    socket.onevent = function (packet) {
      var args = packet.data || [];
      if (!args.length) {
        debug('event args not exist');
        return socket._onevent(packet);
      }
      // <------ inserted
      if (null != packet.id) {
        debug('attaching ack callback to event');
        args.push(this.ack(packet.id));
      }
      // <------- end

      context.event = args[0];
      context.data = args.slice(1);

      co.wrap(gen).call(context, function (err) {
        if (err) console.error(err.stack);
        //TODO: error
        socket._onevent(packet);
      });
    };

    yield* next;
  };
};

Hi!

I just took over this project, and as part of v0.1.0 (#34), I'd like to be able to solve this bug. Could you be a little more specific about the problem you're having?

Thanks!

@ariporad I think this one is absolutely the same as #27 PR. So I'll close this and think for it.