You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UsingRPCToSendEventsToTheClient.asciidoc 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ---
  2. title: Using RPC To Send Events To The Client
  3. order: 71
  4. layout: page
  5. ---
  6. [[using-rpc-to-send-events-to-the-client]]
  7. = Using RPC to send events to the client
  8. An RPC mechanism can be used to communicate from the server to the
  9. client. In effect, the server-side component can call methods that are
  10. executed by the client-side connector. As opposed to shared state
  11. (discussed in a separate article), no information is automatically
  12. re-transmitted when the client-side state is lost (e.g when a browser
  13. reload is invoked).
  14. Whether shared state or RPC is appropriate depends on the nature of the
  15. data being transmitted, but if the information transmitted needs to be
  16. retained on the client over a page refresh, you should probably use
  17. shared state. You'll probably find shared state more appropriate in most
  18. cases, and server-client RPC extremely useful in a few cases.
  19. To set up server-client RPC, we need to create an interface extending
  20. `ClientRpc` for the RPC methods, then register an implementation of the
  21. RPC interface in the client-side connector, and call the method(s) via a
  22. proxy on the server. This is the reverse of the server-client RPC
  23. described in a separate article.
  24. We'll create *MyComponentClientRpc* in the client package:
  25. [source,java]
  26. ....
  27. package com.example.mycomponent.client;
  28. import com.vaadin.shared.communication.ClientRpc;
  29. public interface MyComponentClientRpc extends ClientRpc {
  30. public void alert(String message);
  31. }
  32. ....
  33. Again, note that the RPC methods can not return anything, but can take
  34. multiple arguments.
  35. In *MyComponentConnector* we register the RPC implementation in the
  36. constructor. This time we'll create the implementation inline:
  37. [source,java]
  38. ....
  39. package com.example.mycomponent.client;
  40. // imports removed for clarity
  41. @Connect(MyComponent.class)
  42. public class MyComponentConnector extends AbstractComponentConnector {
  43. MyComponentServerRpc rpc = RpcProxy
  44. .create(MyComponentServerRpc.class, this);
  45. public MyComponentConnector() {
  46. registerRpc(MyComponentClientRpc.class, new MyComponentClientRpc() {
  47. public void alert(String message) {
  48. Window.alert(message);
  49. }
  50. });
  51. /* The rest of the code remains unchanged:
  52. getWidget().addClickHandler(new ClickHandler() {
  53. public void onClick(ClickEvent event) {
  54. final MouseEventDetails mouseDetails = MouseEventDetailsBuilder
  55. .buildMouseEventDetails(event.getNativeEvent(),
  56. getWidget().getElement());
  57. rpc.clicked(mouseDetails);
  58. }
  59. });
  60. }
  61. @Override
  62. protected Widget createWidget() {
  63. return GWT.create(MyComponentWidget.class);
  64. }
  65. @Override
  66. public MyComponentWidget getWidget() {
  67. return (MyComponentWidget) super.getWidget();
  68. }
  69. @Override
  70. public MyComponentState getState() {
  71. return (MyComponentState) super.getState();
  72. }
  73. @OnStateChange("text")
  74. void updateText() {
  75. getWidget().setText(getState().text);
  76. }
  77. */
  78. }
  79. ....
  80. (`MyComponentServerRpc` is introduced in
  81. <<SendingEventsFromTheClientToTheServerUsingRPC#, Sending
  82. events from the client to the server using RPC>>. `Window` here is
  83. `com.google.gwt.user.client.Window`, _not_ `com.vaadin.ui.Window`.)
  84. Finally, in *MyComponent* we use the RPC via a proxy:
  85. [source,java]
  86. ....
  87. import com.vaadin.ui.AbstractComponent;
  88. public class MyComponent extends AbstractComponent {
  89. private int clickCount = 0;
  90. private MyComponentServerRpc rpc = new MyComponentServerRpc() {
  91. public void clicked(MouseEventDetails mouseDetails) {
  92. clickCount++;
  93. // nag every 5:th click
  94. if (clickCount % 5 == 0) {
  95. getRpcProxy(MyComponentClientRpc.class).alert(
  96. "Ok, that's enough!");
  97. }
  98. // setText("You have clicked " + clickCount + " times");
  99. }
  100. };
  101. /* Unchanged code follows:
  102. public MyComponent() {
  103. registerRpc(rpc);
  104. }
  105. @Override
  106. public MyComponentState getState() {
  107. return (MyComponentState) super.getState();
  108. }
  109. public void setText(String text) {
  110. getState().text = text;
  111. }
  112. public String getText() {
  113. return getState().text;
  114. }
  115. */
  116. }
  117. ....
  118. That is: every fifth time the label is clicked, we get the RPC proxy by
  119. calling `getRpcProxy()` and call our `alert()` method with a message to
  120. send to the client.
  121. Compile the widgetset, and you're all set to try out server-client RPC.