我正在尝试使用此源代码从GWT项目中获取U2F Token的响应:
public class Test implements EntryPoint {
@Override
public void onModuleLoad() {
Window.alert("Alert 3:"+u2FTest());
}
public static native String u2FTest()/*-{
var respond = {rep: "Clear"};
var RegistrationData = {"challenge":"dG7vN-E440ZnJaKQ7Ynq8AemLHziJfKrBpIBi5OET_0",
"appId":"https://localhost:8443",
"version":"U2F_V2"};
$wnd.u2f.register([RegistrationData], [],
function(data) {if(data.errorCode) {
alert("U2F failed with error: " + data.errorCode);
return;
}
respond.rep=JSON.stringify(data);
alert("Alert 1: "+respond.rep);
});
alert("Alert 2: "+respond.rep);
return respond.rep;
}-*/;
}
由于某些原因,我得到的警告是这样的:
通常我会得到(警报1)令牌响应然后2,3。那么我怎么能停止执行,直到我得到令牌响应 谢谢,
答案 0 :(得分:2)
拥抱异步性!
public static native void u2FTest(com.google.gwt.core.client.Callback<String, Integer> callback) /*-{
// …
$wnd.u2f.register(regReqs, signReqs, $entry(function(response) {
if (response.errorCode) {
callback.@com.google.gwt.core.client.Callback::onFailure(*)(@java.lang.Integer::valueOf(I)(response.errorCode));
} else {
callback.@com.google.gwt.core.client.Callback::onSuccess(*)(JSON.stringify(response));
}
}));
}*-/;
(不要忘记在$entry()
中回放回调,以便将异常路由到GWT.UnhandledExceptionHandler
,如果有的话)