I have created a service in SAP backend that serves for websocket connections. I wanna connect to that service from SAPUI5 application. I connect to the service like this:
sap.ui.require("sap/ui/core/ws/WebSocket");
var socket = new WebSocket(
'ws://<host>:<port>/sap/bc/apc/sap/z_reg_test_push_ch');
When this piece of code is called I get:
WebSocket connection to 'ws://<host>:<port>/sap/bc/apc/sap/z_reg_test_push_ch' failed: HTTP Authentication failed; no valid credentials available
When I test the service from backend it will open the service url in chrome and asks me for credentials. Then when I refresh the UI5 app the webservice connection is established because the user has already logged in the same session in chrome when testing the service from backend.
In SAPUI5 app I can access the security token which was returned when connecting to oData service. I can access is like this:
var oModel = sap.ui.getCore().getModel();
oModel.getSecurityToken();
the token looks like this: cKYPxpo7-BELSah1D1WRgg==
I have found a thread that I should create the websocket connection like this:
new WebSocket('ws://<host>:<port>/sap/bc/apc/sap/z_reg_test_push_ch',["access_token", oModel.getSecurityToken()]);
But I get this message:
Uncaught DOMException: Failed to construct 'WebSocket': The subprotocol 'cKYPxpo7-BELSah1D1WRgg==' is invalid.
To sum it up. I am authenticated by the oData service via basic auth and I want to pass to the socket the authentication info.