1

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.

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You are missing the $wnd prefix

return new $wnd.WebSocket(url);

Comments

0

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?

4 Comments

The goal for me is not to get a working implementation of WebSockets, but to figure out why I am getting this error. My implementation and the gwt-comet one are nearly identical, but he apparently isn't getting this exception. I am sure I will run into this problem again if I don't figure out the reason for it.
I tried your code and I don't get the error. Actually it works if I try it with a real websocket server Socket socket = Socket.connect("ws://echo.websocket.org")
That was unexpected. I am using the GWT4NB plugin for NetBeans which was designed to work with the sdk up to version 2.0, but it is currently using the 2.3 sdk. Would you mind telling me which IDE you are using and which version of the SDK?
I have migrated my code to IntelliJ, and despite the fact that I got the exact same exception first time running it, I now have far better feedback from the compiler, and was able to resolve my issues. Thank you for your help.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.