开发者

Problem integrating JavaScript into GWT with JSNI

I am trying to create a simple binding for WebSockets for GWT using JSNI, but I keep getting an exception every time a JSNI method is invoked. The simplified class definition is as fallows:

public class Socket extends JavaScriptObject{
    protected Socket() {}

    public static native Socket connect(String url) /*-{
        return new WebSocket(url);
    }-*/;
}

While trying to instantiate a Socket object, using the line :

Socket socket = Socket.connect("http://www.google.com");

i get the fallowing exception and I don't know why :

   java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessor开发者_StackOverflowImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:396)
    at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:193)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:510)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:352)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassFormatError: Illegal method name "$" in class edu/catalindumitru/gwt/socket/Socket
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:465)
    at com.google.gwt.dev.shell.CompilingClassLoader.findClass(CompilingClassLoader.java:1078)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at edu.catalindumitru.gwt.steel.client.GameCore.onModuleLoad(GameCore.java:32)
    ... 9 more

I have tried previously to create a similar binding for typed arrays and web workers, but I got the exact same error, so I decided to suspend development for those bindings and try something simpler until I can find the reason for this exception.


I got the same error report from GWT when using JavaScriptObject's quite conventionally.

My problem was simply that all JSO methods must be declared final, and I had missed one.


You are missing the $wnd prefix

return new $wnd.WebSocket(url);


Take a look how others have implemented it: http://code.google.com/p/gwt-comet/source/browse/trunk/src/net/zschech/gwt/websockets/client/WebSocket.java

Maybe you could just use gwt-comet's websockets?


Just a guess, but as far as I seen the overlays were used rather for JSON type data. Here your try to perform marshaling for browser API object. I'm not sure if it is going to work this way. Especially that JSNI have restrictions of types being passed.

Maybe you could try storing WebSocket as a field in native part, and delegate methods to that field. But this is just a blind guess.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜