Cataclysm
Cataclysm

Reputation: 8598

Vaadin 7 : RPC call from Server to Client

I would like to create custom image component .So I followed step by step from Integrating with Server-side. For basic or first-step , I created for the test Server to Client RPC call as following steps.

MyComponentWidget.java

public class MyComponentWidget extends HTML {

public MyComponentWidget() {
    getElement().setAttribute("class", "thumbnail");
}

public final void createCustomImage(final String url) {
 getElement().setInnerHTML("<div class='delete-block'></div><img src=" + url + " />");
}
}

MyComponentState.java

public class MyComponentState extends AbstractComponentState {
private String url;
private String html;

public final String getUrl() {
    return url;
}

public final void setUrl(final String url) {
    this.url = url;
}

public final String getHtml() {
    return html;
}

public final void setHtml(final String html) {
    this.html = html;
}
}

MyComponentConnector.java

public class MyComponentConnector extends AbstractComponentConnector {

public MyComponentConnector() {
    registerRpc(MyComponentClientRpc.class, new MyComponentClientRpc() {

        @Override
        public void getMessage() {
            // never reach to this place
            System.err.println("Reach Here !");
            getState().setHtml(getWidget().getHTML());
        }
    });
}

@Override
public final MyComponentWidget getWidget() {
    return (MyComponentWidget) super.getWidget();
}

@Override
public final MyComponentState getState() {
    return (MyComponentState) super.getState();
}

@OnStateChange("url")
final void updateText() {
    getWidget().createCustomImage(getState().getUrl());
}
}

MyComponentClientRpc.java

import com.vaadin.shared.communication.ClientRpc;
public interface MyComponentClientRpc extends ClientRpc {
void getMessage();
}

MyComponent.java

public class MyComponent extends AbstractComponent {

public MyComponent(final String url) {
    getState().setUrl(url);
}

public final MyComponentState getState() {
    return (MyComponentState) super.getState();
}

public final String getHTML() {
    getRpcProxy(MyComponentClientRpc.class).getMessage();
    return getState().getHtml();
}
}

and call as

MyComponent image = new MyComponent("myImageUrl");
System.out.println(image.getHTML());

My problem is why I always get null value at my console ? I can see the image at browser but System.out.println(image.getHTML()); produces null. What am I missing ?

Upvotes: 1

Views: 1693

Answers (1)

Henri Kerola
Henri Kerola

Reputation: 4967

To make a rpc call from client to server, you must extend the ServerRpc interface, for example:

package com.example.client.MyServerRpc

public interface MyServerRpc extends com.vaadin.shared.communication.ServerRpc {
    void sendHTML(String html);
}

In your connector your register the rpc:

private MyServerRpc rpc = RpcProxy.create(MyServerRpc.class, this);

And then you can send a value by using the registered rpc in your connector:

 rpc.sendHTML(html);

To receive the value on your component's or extension's server-side class, you must create an instance of the rpc interface:

private MyServerRpc rpc = new MyServerRpc() {
    @Override
    public void sendHTML(String html) {
        // this method will be called!
    }

};

and register that in the constructor:

registerRpc(rpc);

After these steps RPC from client to server should work.

Upvotes: -1

Related Questions