Search code examples
gwtwebsocketjsni

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(NativeMethodAccessorImpl.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.


Solution

  • 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?