ABAP Keyword Documentation → ABAP − Reference → Data Interfaces and Communication Interfaces → ABAP Channels → APC - ABAP Push Channels → Examples of APC
APC, AS ABAP as WebSocket Server
This example demonstrates ABAP push channels in AS ABAP as a WebSocket server.
Other versions:
7.31 | 7.40 | 7.54
Source Code
IF url IS INITIAL.
RETURN.
ENDIF.
DATA amc TYPE abap_bool VALUE 'X'.
cl_demo_input=>add_field( EXPORTING as_checkbox = 'X'
CHANGING field = amc ).
cl_demo_input=>add_line( ).
DATA pcp TYPE abap_bool VALUE ' '.
cl_demo_input=>add_field( EXPORTING as_checkbox = 'X'
CHANGING field = pcp ).
DATA pcp_stateful TYPE abap_bool VALUE ' '.
cl_demo_input=>add_field( EXPORTING as_checkbox = 'X'
CHANGING field = pcp_stateful ).
cl_demo_input=>request( ).
amc = to_upper( amc ).
cl_demo_output=>display_html(
|<html>| &&
|<body>| &&
|<a href="{ COND string( WHEN pcp IS INITIAL AND
pcp_stateful IS INITIAL THEN url
ELSE url && `_pcp` )
}?sap-client={ sy-mandt
}&sap-language={ cl_i18n_languages=>sap1_to_sap2( sy-langu )
}{ COND string( WHEN amc = abap_true
THEN `&amc=x`
ELSE `` )
}{ COND string( WHEN pcp_stateful = abap_true
THEN `&pcp_stateful=x`
ELSE `` )
}" target="_blank">| &&
|Open demo web page with standard browser</a>
| &&
|If your browser does not support WebSocket, | &&
|copy the URL to another browser.| &&
|</body>| &&
|</html>| ).
Description
This example demonstrates bidirectional communication between a browser that supports the WebSocket protocol as an APC client and AS ABAP as an APC server. The example program above has the task of displaying a webpage that communicates with the current AS ABAP. It is possible to specify the following:
- Is the APC used associated with an ABAP messaging channel (AMC)?
- Is an APC used together with the Push Channel Protocol (PCP) or is an APC used without this protocol?
- Is a stateful service used when the subprotocol PCP is used?
The Web page is provided by an ICF HTTP service whose URL is constructed in the program for the current AS Instance. If the standard browser called does not support communication using the WebSocket protocol, you must open the URL using a suitable browser instead.
The communication partners are:
- The Web pages generated in the HTTP request handler CL_HTTP_EXT_APC_DEMO or CL_HTTP_EXT_APC_PCP_DEMO. One Web page makes it possible to open a WebSocket object without Push Channel Protocol (PCP) support and the other Web page makes it possible to open a WebSocket object with the support of this protocol. Both Web pages make it possible to send messages using the object and to close the object. These tasks are performed by a JavaScript function implemented in the Web page. If PCP is used, a JavaScript file sap-pcp-websocket.js is included from the Mime repository in AS ABAP. This file provides the client-side framework for using this protocol. This API is not needed if PCP is not used. If the PCP protocol is used, it is possible to access both a stateless ABAP channel and a stateful channel.
- The ABAP push channel DEMO_APC, DEMO_APC_PCP, or DEMO_APC_PCP_STATEFUL from the package SABAPDEMOS in the current AS ABAP (with the latter two supporting the Push Channel Protocol (PCP) and the latter being stateful). The URL of the service assigned to the push channel is used in the JavaScript function of the associated Web page to open the WebSocket communication of this Web page. The form field amc is passed further. The APC handler classes CL_APC_WS_EXT_DEMO_APC and CL_APC_WSP_EXT_DEMO_APC_PCP implement the methods ON_START and ON_MESSAGE of the interface IF_APC_WSP_EXTENSION for communication with and without a connection to an AMC. The APC handler class CL_APC_WSP_EXT_DEMO_APC_PCP_ST demonstrates a stateful service by simply calling the identically named methods of an object of the class CL_APC_WSP_EXT_DEMO_APC_PCP. The AMC used here is /demo_text or /demo_pcp from the executable examples for AMC. When a message is received, the method ON_MESSAGE creates a new message with information about the received message and sends it. If PCP is not used, a text message is sent. In the version for PCP, this protocol is used. CL_APC_WSP_EXT_DEMO_APC_PCP demonstrates the stateful service by raising a counter whose value stays the same in stateless services.
If the ABAP push channel is opened using the form field amc=x, communication takes place using AMC. Each Web page bound to the push channel receives every message sent using AMC. In the example, this means that the message sent by ON_MESSAGE is not just received by the Web page that sent a message. Furthermore, the Web pages bound to the push channel receive messages sent, for example, using the program DEMO_SEND_AMC from the executable example Sending AMC Messages. Conversely, the program DEMO_RECEIVE_AMC receives the messages sent from the Web pages from the executable example Receiving AMC Messages.
Note
Before this example can work, the protocols WEBSOCKET and WEBSOCKETS must be specified as active services in the Services function in the transaction SMICM.