Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding of broadcasted messages does not work reliably with multiple @Message annotations #1443

Closed
suffix77 opened this issue Jan 16, 2014 · 2 comments
Labels

Comments

@suffix77
Copy link

Within a @managedservice, I have multiple methods annotated with @message, all but one of them having just decoders, i.e. for incoming messages. The only messages the server is sending is "update-type" messages to all connected clients (one per client) in an "update" method. Please see the example code below for details.

What happens is that in org.atmosphere.config.managed.ManagedAtmosphereHandler:178 a suitable encoder for "Update" is looked for:

[...]
} else {
  Object msg = event.getMessage();
  Object o;
  // No method matched. Give a last chance by trying to decode the proxiedInstance.
  // This makes application development more simpler.
  // Chaining of encoder is not supported.
  // TODO: This could be problematic with String + method
  for (MethodInfo m : onRuntimeMethod) {
    o = Invoker.encode(encoders.get(m.method), msg);
    if (o != null) {
      event.setMessage(o);
      break;
  }
}
[...]

However, if - for whatever reason - the for-MethodInfo loop here comes across one of the "decoder-only" @Message-annotated methods first, it will just encode the class with the default "toString" method (which is useless, obviously) rather than the (Jackson) UpdateEncoder I'd want to use. What fixes it is adding the UpdateEncoder to all "decoder-only" methods

Sample code:

@Message(encoders = { UpdateEncoder.class }, decoders = { UpdateDecoder.class })
public Update onMessage(AtmosphereResource r, Update update) {
  return playerGameState; 
}

@Message(decoders = { ExampleADecoder.class })
public void onMessage(AtmosphereResource r, ExampleA exampleA) throws IOException {
  // do stuff with exampleA
  sendUpdates();
}

@Message(decoders = { ExampleBDecoder.class })
public void onMessage(AtmosphereResource r, ExampleB exampleB) throws IOException {
  // do stuff with exampleB
  sendUpdates();
}

@Message(decoders = { ExampleCDecoder.class })
public void onMessage(AtmosphereResource r, ExampleC exampleC) throws IOException {
  // do stuff with exampleC
  sendUpdates();
}

private void sendUpdates() {
  for (String uuid : uuids.keySet()) {
    AtmosphereResource r = AtmosphereResourceFactory.getDefault().find(uuid);
    Update update = <call a method to getUpdateForUuid>;
    r.getBroadcaster().broadcast(update, r);
  } 
}

Google groups thread:
https://groups.google.com/forum/?hl=en#!topic/atmosphere-framework/thG8zItZXvE

@jfarcand
Copy link
Member

I'm having a hard time reproducing the issue...can you share the change you did? Or an app that reproduce the issue. Thanks!

@jfarcand
Copy link
Member

OK Got It reproducible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants