WebSocket React Native Invalid error: Invalid Sec-WebSocket-Accept response - spring

I am receiving an Invalid Sec-WebSocket-Accept response when client (running on a simulator) tries to establish connection to the server. My server is written in Spring boot - java (websocket), client react native. I am using WebSocket to establish the connection to the server. This is what my client code looks like
const client = new WebSocketClient('ws://localhost:5000/websocket');
Result:
Event {
"isTrusted": false,
"message": "Invalid Sec-WebSocket-Accept response",
}
I have tried everything but I keep getting this error. I tested the connection to a mock server ws://echo.websocket.events/ and it works fine - I am confused on what I am doing wrong. I also tested this with ws://10.0.2.2:5000/websocket and i get
Event {
"isTrusted": false,
"message": "The operation couldn’t be completed. Connection refused",
}
help :)
NOTE: everything works fine on a browser

Related

Connect Laravel echo from Angular project

I have a Laravel-echo-server with Redis running on my local.
I created a test API endpoint, that emits broadcastable event.
on http://localhost:8000/api/web-socket-test I see the response in echo server CLI.
I set-up laravel-echo auth key and I can get the stat info from server API
http://localhost:6001/apps/APP_ID/status?auth_key=b73a61d0.
The problem is with connecting to echo-server from Angular via ws: protocol.
My connection code is
import {webSocket, WebSocketSubject} from 'rxjs/webSocket';
export class MyComponent implements OnInit, OnDestroy {
myWebSocket: WebSocketSubject<any> = webSocket('ws://127.0.0.1:6001');
ngOnInit() {
this.myWebSocket.subscribe(
msg => console.log('message received: ' + msg),
err => console.log(err),
() => console.log('complete')
);
}
And finally I've got an error: WebSocket connection to 'ws://127.0.0.1:6001/' failed: Connection closed before receiving a handshake response.
How can I establish ws connection?
I believe you want to try connecting using the socket.io client libraries instead of using rxjs raw websockets.
Although it's not immediately clear from the laravel echo server docs, the project title states it's a 'Socket.io server for Laravel Echo'. So I'm assuming you should use the socket.io client libraries for connections.

How to listen to Server-sent Events on SAPUI5

I'm trying to make my SAPUI5 app deployed on Sap Cloud Platform listen to server-sent events sent from a simple test server.
I found this tutorial: https://auth0.com/blog/developing-real-time-web-applications-with-server-sent-events/
The tutorial explains how to create a React app that displays a table with some information and how to use server-sent events to make the server update the information on the client without the client having to make periodic requests to the server.
I tested it and got it working, and now I'm trying to substitute the React client app for my SAPUI5 app.
I added a HANA destination in my sap account and added the destination in my neo-app file.
This is basically the controller for the app:
onInit: function () {
this.eventSource = new EventSource('/flightinfo/events');
this.eventSource.addEventListener('flightStateUpdate', this.updateFlightState, false);
}
updateFlightState: function(event) {
console.log("Hello");
}
And this is how the destination is declared in the neo-app.json file:
{
"path": "/flightinfo",
"target": {
"type": "destination",
"name": "flightinfo"
},
"description": "flightinfo"
}
I expected my app to print to the console every time the server sent an update, since I'm just testing the connection and not using the data received. However, the app doesn't fire the event that would trigger the console output.
My server log shows the request is received and the response is 200 OK, but the webapp shows the response as timed out:
HTTP Status 504 - Socket connection timed out for host [my server]. Reason: Read timed out (local port [port number] to address [ip address] ([...].od.sap.biz), remote port [port number] to address [ip address] ([...].od.sap.biz))
I redacted IPs and Ports from the error message.
Trying to open the request url from the network tab at chrome responds with:
HTTP Status 503 - No application is available to handle this request
However, navigating manually to the address of my server (the address I introduced in the Destinations tab on SCP) does show me the data that my server is sending, updates and all.
What am I getting wrong? Is there some security policy preventing my server from sending server-sent events through a SCP Destination?
Any help would be greatly appreciated, since I can't seem to find any information about using server-sent events on SAPUI5 anywhere.
Thank you!

Spring Websocket 400 Error: Handshake failed due to invalid Upgrade header: null

I am using wss (secured web sockets) with spring 4.2.x from backend and STOMP for javascript client. I can run it successfully under http, but failed when under https.
The only error I met is:
ERROR o.s.w.s.s.s.DefaultHandshakeHandler - Handshake failed due to invalid Upgrade header: null
The JS error is:
WebSocket connection to 'wss://.example.com/.../websocket' failed: Error during WebSocket handshake: Unexpected response code: 400
I googled a lot and understand that I should do some configuration on wildfly to make it support wss requests. But I don't know how. Thanks in advance if someone can give any idea!

Spring what is between Controller and Interceptor

I have a controller that is doing something, everything seems fine, it logs a success, and then it does this
return new ResponseEntity<>(resp, HttpStatus.OK);
Seems like it should send a 200 OK.
Well, I also have a HandlerInterceptorAdapter with an "afterCompletion" that logs the status:
logger.debug("Response status: {}, URI: {}", response.getStatus(), request.getRequestURI());
This is showing a 500 where there was a success from the controller.
In the stdout logs I see an error
2018-01-12 12:33:59.035 ERROR 18952 --- [pr-8080-exec-14] o.s.boot.web.support.ErrorPageFilter : Cannot forward to error page for request [/ws/path] as the response has already been committed.
As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false
and
org.apache.catalina.connector.ClientAbortException: java.io.IOException: APR error: -32
I can't find any doc on what that APR error is and I have no idea why the status code would be wrong. I am using tomcat 8.5.
So. APR -32 is a "Client Abort" error. The client was timing out in waiting. It looks like what was happening was the client made a request and was sitting in queue for a few seconds. It has a short hard timeout limit. The client timed out before the server got to the request. Then the server picked up the request and did everything fine but when trying to write back to the client, it found the socket closed. So it set a 500 and threw that exception.

Websocket implementation not working in Apache Ofbiz

I am checking the Websocket implementation in Apache Ofbiz.
I followed the parent ticket OFBIZ-7073 and its child tickets.
But I am getting the following error-
ExamplePushNotifications.js:23 WebSocket connection to 'wss://localhost:8443/example/ws/pushNotifications' failed: Error during WebSocket handshake: Unexpected response code: 404
I think that the request is redirecting to control servlet rather than tomcat server. Therefore IMO, we need to make some changes in example/web.xml.
How should I proceed?
Thanks in advance!

Resources