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.

SendingEventsFromTheClientToTheServerUsingRPC.asciidoc 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ---
  2. title: Sending Events From The Client To The Server Using RPC
  3. order: 20
  4. layout: page
  5. ---
  6. [[sending-events-from-the-client-to-the-server-using-RPC]]
  7. = Sending events from the client to the server using RPC
  8. An RPC mechanism can be used to communicate from the client to the
  9. server. In effect, the client can call methods that are executed by the
  10. server component. The server component can then take appropriate action
  11. - e.g updating the shared state or calling event listeners.
  12. To set up client-server RPC we need to create one interface defining the
  13. RPC methods, and then make use of that interface on both the client and
  14. the server. Place the `MyComponentServerRpc` interface in the client
  15. package:
  16. [source,java]
  17. ....
  18. package com.example.mycomponent.client;
  19. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  20. import com.vaadin.terminal.gwt.client.communication.ServerRpc;
  21. public interface MyComponentServerRpc extends ServerRpc {
  22. public void clicked(MouseEventDetails mouseDetails);
  23. }
  24. ....
  25. Note that the RPC methods can not have return values. In this example,
  26. we pass `MouseEventDetails` to get a more complete example, but you
  27. could pass almost any (or no) parameters.
  28. In the server side `MyComponent` we need to implement the interface, and
  29. register it for use:
  30. [source,java]
  31. ....
  32. package com.example.mycomponent;
  33. import com.example.mycomponent.client.MyComponentServerRpc;
  34. import com.example.mycomponent.client.MyComponentState;
  35. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  36. import com.vaadin.ui.AbstractComponent;
  37. public class MyComponent extends AbstractComponent {
  38. private int clickCount = 0;
  39. private MyComponentServerRpc rpc = new MyComponentServerRpc() {
  40. public void clicked(MouseEventDetails mouseDetails) {
  41. clickCount++;
  42. setText("You have clicked " + clickCount + " times");
  43. }
  44. };
  45. public MyComponent() {
  46. registerRpc(rpc);
  47. }
  48. /* Previous code commented out for clarity:
  49. @Override
  50. public MyComponentState getState() {
  51. return (MyComponentState) super.getState();
  52. }
  53. public void setText(String text) {
  54. getState().text = text;
  55. }
  56. public String getText() {
  57. return getState().text;
  58. }
  59. */
  60. }
  61. ....
  62. Here we react to the RPC call by incrementing a counter. We do not make
  63. use of the `MouseEventDetails` (yet). Notice the *important call to
  64. `registerRpc()`* in the added constructor.
  65. In the client side `MyComponentConnector`, we use `RpcProxy` to get an
  66. implementation of the RPC interface, and call the `clicked()` method
  67. when the widget is clicked:
  68. [source,java]
  69. ....
  70. package com.example.mycomponent.client;
  71. // imports removed for clarity
  72. import com.vaadin.terminal.gwt.client.communication.RpcProxy;
  73. @Connect(MyComponent.class)
  74. public class MyComponentConnector extends AbstractComponentConnector {
  75. MyComponentServerRpc rpc = RpcProxy
  76. .create(MyComponentServerRpc.class, this);
  77. public MyComponentConnector() {
  78. getWidget().addClickHandler(new ClickHandler() {
  79. public void onClick(ClickEvent event) {
  80. final MouseEventDetails mouseDetails = MouseEventDetailsBuilder
  81. .buildMouseEventDetails(event.getNativeEvent(),
  82. getWidget().getElement());
  83. rpc.clicked(mouseDetails);
  84. }
  85. });
  86. }
  87. /* Previous code commented out for clarity:
  88. @Override
  89. protected Widget createWidget() {
  90. return GWT.create(MyComponentWidget.class);
  91. }
  92. @Override
  93. public MyComponentWidget getWidget() {
  94. return (MyComponentWidget) super.getWidget();
  95. }
  96. @Override
  97. public MyComponentState getState() {
  98. return (MyComponentState) super.getState();
  99. }
  100. @OnStateChange("text")
  101. void updateText() {
  102. getWidget().setText(getState().text);
  103. }
  104. */
  105. }
  106. ....
  107. Notice that most of the code is for attaching the click handler and
  108. creating the `MouseEventDetails`, the code for the actual RPC is quite
  109. minimal.
  110. Compile the widgetset, and the label text should be updated with the
  111. click count whenever you click it (remember that the counting is done on
  112. the server-side).
  113. Finally, note that you can use multiple RPC interfaces in one component,
  114. allowing for better code separation and reuse.
  115. You can do the same thing in the other direction, see the article about
  116. server to client RPC for details.