All the magic done through Sockets and the Browser Plugin.
Design: Out of Process Hosted Mode (OOPHM)
Here is the Essential part.
Consider the following GWT code:
public class MyEntryPoint implements EntryPoint {
private static native int jsniMethod() /*-{
return 1;
}-*/;
public void onModuleLoad() {
jsniMethod();
}
}
JavaScript: the browser plugin sends a LoadModuleMessage with the module name.
Java: the hosted mode server receives the LoadModuleMessage, loads the module and invokes the onModuleLoad in the corresponding EntryPoints. In this case MyEntryPoint::onModuleLoad is called. When MyEntryPoint is compiled, a LoadJsniMessage is sent to create browser-side JavaScript functions for each JSNI method, then when onModuleLoad invokes jsniMethod an InvokeMessage is sent.
JavaScript: This is the key part of the example. The JavaScript engine is currently awaiting a return from the LoadModuleMessage it sent, but it must be in a position to invoke the call to MyEntryPoint::jsniMethod on the same thread. This is accomplished by having the thread enter a read-and-dispatch routine following every remote invocation. In this case, the thread receives the LoadJsniMessage and InvokeMessage messages, invokes jsniMethod and sends a ReturnMessage containing the value 1.
Java: The read-and-dispatch routine receives the ReturnMessage and knows to return from the call to jsniMethod. Having fully executed the onModuleLoad method it sends a ReturnMessage and falls back into a top level read-and-dispatch loop. (Since all calls originate from the browser's UI event dispatch, only the hosted mode server needs to remain in a read-and-dispatch routine during idle time. The browser simply returns control by exiting the JavaScript function that was originally called.)