1

I'm Tinoue, I want to write these in JSNI method for using 'message' in GWT.

private native void setUp(JavaScriptObject method) /*-{
    window.addEventListener('message', method, false); 
}-*/;

My hope is invoke some method when some message come. Especially the method that is written outside of JSNI.

All of codes:

public void setUpMessaging () {
    setUp(get());
}

private native JavaScriptObject get () /*-{
    return this.@somePackage::mtd(Lcom/google/gwt/user/client/Event;);
}-*/;

public static void mtd (Event e) {
    //some description,,,
}

private native void setUp (JavaScriptObject method) /*-{
    window.addEventListener('message', method, false); 
}-*/;

But now, the "mtd" method never work without "static". This is normal, of course I know.

But I don't want to using "static" here.

Someone know some solution?

Regards.

Tinoue
  • 65
  • 1
  • 6
  • 1
    Do you want to use the browser native html5 onmessage/message event (http://dev.w3.org/html5/postmsg/) or simply want to create your own event to pass information in your application? – Hilbrand Bouwkamp Jan 06 '11 at 09:48
  • What I want is using 'message'-event for messaging. – Tinoue Jan 06 '11 at 10:23
  • Is there some other solution? GWT's Class "Window" does not support HTML5 onmessage function yet. or not? – Tinoue Jan 06 '11 at 10:26

3 Answers3

1

If you want to implement a messaging application in GWT you might want to look at comet instead of reinventing the wheel. See this stackoverflow answer for some startingpoint on GWT/comet: GWT / Comet: any experience?

EDIT: To create a custom event, simply take an existing GWT event like PlaceChangeEvent. Let's assume the event is called MessageEvent:

// (copy from PlaceChangeEvent)
public class MessageEvent extends GwtEvent<MessageEvent.Handler> 

 public interface Handler extends EventHandler {
   void onMessage(MessageEvent event);
  }
  //... rest like PlaceChangeEvent, but with PlaceChange replaced with Message
  // and instead of Place or use String message or a more advanced Message you create,
  // which contains the message data.
}

Now for all the classes that need to act on this event, register them via a global eventbus instance:

  //where this implements the MessageEvent.Handler interface
  myglobaleventbus.addHandler(MessageEvent.TYPE, this); 

And to send message fire an event:

  myglobaleventbus.fire(new MessageEvent(message));
Community
  • 1
  • 1
Hilbrand Bouwkamp
  • 13,509
  • 1
  • 45
  • 52
  • Thanxs! So I saw GWT/comet before, but my purpose is Client-Local-Work only. Without server. When there are some module in client, I wanna connect these possibly light & easy with DI. Eventually I want to make something like NSNotification @ Objective-C. – Tinoue Jan 06 '11 at 14:53
  • You mean simply sending a message event within your GWT application? – Hilbrand Bouwkamp Jan 06 '11 at 15:30
  • YES. You said, "use the browser native html5 onmessage/message event" , is called "WebSocket", isn't it?. I wanna just use window.postmessage() method in GWT for local-messaging. – Tinoue Jan 06 '11 at 16:00
  • In that case you should just use a GWT version and not use the html5 message, it's intended for client/server communication. I'll give the answer in a new comment. – Hilbrand Bouwkamp Jan 06 '11 at 16:04
  • Thanks a lot. I'll continue finding some other approach. – Tinoue Jan 06 '11 at 16:12
  • So, now I got a solution. http://twitter.com/toru_inoue/status/23312114806423552 Thanks a lot! – Tinoue Jan 07 '11 at 09:37
  • The things I did is, use "static" & use EventBus. I noticed that when event through "EventBus", the event will become "non-static". Thank you all. – Tinoue Jan 07 '11 at 09:49
0

From what I can tell you are trying to create a custom event type and handler. I would suggest that rather than doing a lot of JSNI that you try an all GWT approach specified here. In regards to your original question this workaround to passing a function reference does not for java. This is what an interface is for. Require an interface as the input to the setUp method that contains the method mtd. Then in the handler registration instead of passing the function directly you can create a javascript function which makes the jsni call to avoid wiredness in the gwt jsni compiler handing function references.

Community
  • 1
  • 1
LINEMAN78
  • 2,562
  • 16
  • 19
0

The case closed and the result is below.

https://github.com/KISSAKI-TechHub/MessengerGWT

thanks a lot.

Tinoue
  • 65
  • 1
  • 6