Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Data Interfaces and Communication Interfaces →  ABAP Channels →  APC - ABAP Push Channels →  Examples of APC 

APC, AS ABAP as WebSocket Client

This example demonstrates AS ABAP as a WebSocket client.

Other versions: 7.31 | 7.40 | 7.54

Source Code

REPORT demo_apc_client.

CLASS apc_handler DEFINITION FINAL.
  PUBLIC SECTION.
    INTERFACES if_apc_wsp_event_handler_pcp.
    DATA       message TYPE string.
ENDCLASS.

CLASS apc_handler IMPLEMENTATION.
  METHOD if_apc_wsp_event_handler_pcp~on_open.
  ENDMETHOD.

  METHOD if_apc_wsp_event_handler_pcp~on_message.
    TRY.
        me->message = i_message->get_text( ).
      CATCH cx_apc_error INTO DATA(apc_error).
        me->message = apc_error->get_text( ).
      CATCH cx_ac_message_type_pcp_error INTO DATA(pcp_error).
        cl_demo_output=>display( pcp_error->get_text( ) ).
        LEAVE PROGRAM.
    ENDTRY.
  ENDMETHOD.

  METHOD if_apc_wsp_event_handler_pcp~on_close.
    me->message = 'Connection closed!'.
  ENDMETHOD.

  METHOD if_apc_wsp_event_handler_pcp~on_error.
    cl_demo_output=>display( 'Error!' ).
    LEAVE PROGRAM.
  ENDMETHOD.
ENDCLASS.

CLASS apc_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS apc_demo IMPLEMENTATION.
  METHOD main.
    DATA(out) = cl_demo_output=>new( ).

    DATA(msg) = `Hello APC!`.
    cl_demo_input=>add_field( CHANGING field = msg ).
    DATA amc TYPE abap_bool VALUE ' '.
    cl_demo_input=>add_field( EXPORTING as_checkbox = 'X'
                              CHANGING field  = amc ).
    DATA stateful_server TYPE abap_bool VALUE ' '.
    cl_demo_input=>add_field( EXPORTING as_checkbox = 'X'
                             CHANGING field  = stateful_server ).
    DATA messages TYPE i VALUE '5'.
    cl_demo_input=>add_field(  CHANGING field  = messages ).
    DATA wait TYPE i VALUE '10'.
    cl_demo_input=>add_field(  CHANGING field  = wait ).
    cl_demo_input=>request( ).

    TRY.
        DATA(event_handler) = NEW apc_handler( ).

        "Client
        DATA(client) =
          cl_apc_wsp_client_manager=>create_by_destination(
            i_destination = 'NONE'
            i_event_handler = event_handler
            i_protocol =
              if_apc_wsp_event_handler_pcp=>co_event_handler_type ).

        "Server
        DATA(request) =
          client->get_context( )->get_initial_request( ).
        request->set_path_and_query(
          i_relative_uri =
            `/sap/bc/apc/sap/demo_apc_pcp` &&
            COND #(
               WHEN stateful_server = abap_true THEN `_stateful` ) &&
            COND #(
              WHEN amc = abap_true THEN `?amc=x` ) ).
        client->connect( ).

        "Sending messages
        DATA(message_manager) =
          CAST if_apc_wsp_message_manager_pcp(
            client->get_message_manager( ) ).
        DATA(message) =
          CAST if_ac_message_type_pcp(
            message_manager->create_message( ) ).
        TRY.
            IF amc = abap_true.
              message->set_field( i_name = 'amc' i_value = 'x' ).
            ENDIF.
            message->set_text( msg ).
          CATCH cx_ac_message_type_pcp_error INTO DATA(pcp_error).
            cl_demo_output=>display( pcp_error->get_text( ) ).
            LEAVE PROGRAM.
        ENDTRY.
        DO messages TIMES.
          message_manager->send( message ).
        ENDDO.

        "Receiving messages
        DO wait TIMES.
          out->line( ).
          CLEAR event_handler->message.
          WAIT FOR PUSH CHANNELS
               UNTIL event_handler->message IS NOT INITIAL
               UP TO 1 SECONDS.
          IF sy-subrc = 4.
            out->write_text(
              'No handler for APC messages registered' ).
          ELSEIF sy-subrc = 8.
            out->write_text(
              'Timeout occurred!' ).
          ELSE.
            out->write_text(
             |Received APC message: \n\n{
               event_handler->message } | ).
          ENDIF.
        ENDDO.
        out->line( ).

        "Close connection
        client->close( i_reason = 'Application closed connection!' ).

        out->display( ).
      CATCH cx_apc_error INTO DATA(apc_error).
        cl_demo_output=>display( apc_error->get_text( ) ).
        LEAVE PROGRAM.
    ENDTRY.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  apc_demo=>main( ).

Description

This example demonstrates how an ABAP program can create an APC client for the WebSocket protocol.

  • The local class apc_handler, which implements the interface IF_APC_WSP_EVENT_HANDLER_PCP, is used as the handler class. When a message is received, the method on_message sets its message attribute to the message text.
  • CL_APC_WSP_CLIENT_MANAGER is used to create a client object for the current AS ABAP as an APC server and opens a connection to one of its ABAP push channels. It is possible to select which of the two ABAP push channels, DEMO_APC_PCP or DEMO_APC_PCP_STATEFUL, from the executable example AS ABAP as WebSocket Server is used. The server can also be instructed to send its messages using ABAP messaging channels.
  • The message manager of the client object is used to create a message in PCP format and send it multiple times.
  • The statement WAIT FOR PUSH CHANNELS is then used to switch the program to a wait state so that messages sent back from the server can be handled. Here, the logical expression checks the attribute message of the handler class apc_handler. A received text is displayed.
  • Finally, the connection is closed explicitly.

It is possible to select how many messages are sent and how often the wait state occurs.

  • If communication takes place with the stateful server, the counter level of the server is raised, indicating that multiple messages are being sent and received.
  • If the server sends its messages using ABAP messaging channels, other AMC receivers in the same messaging channel can also see the messages of the server. For example, the Web browser from the executable example AS ABAP as WebSocket Server can receive these messages. In the reverse direction, the APC client can then also receive those messages sent by the APC server as a response to messages from the Web browser, if they occur in one of the programmed wait periods.