WrathChaos / StompClientLib

Simple STOMP Client library, Swift 3 and 4, 4.2, 5 compatible
https://www.freakycoder.com
MIT License
153 stars 81 forks source link

Can't connect to websocket : received bad response code from server 422 #80

Closed tomasimartin closed 4 years ago

tomasimartin commented 4 years ago

Hi. I get an error while trying to connect to websocket, here is the code:

if let url = NSURL(string: hostws) {
            socketClient.openSocketWithURLRequest(request: NSURLRequest(url: url as URL), delegate: self)
        }

Response:

Error Domain=SRWebSocketErrorDomain Code=2132 "received bad response code from server 422" UserInfo={NSLocalizedDescription=received bad response code from server 422, HTTPResponseStatusCode=422}

And if I use another "Starscream" library, it connects very well with the same url, but I need the "STOMP" part that this other library doesn't have

Do you have any ideas why that might be the case?

WrathChaos commented 4 years ago

Hello @tomasimartin, Thank you for using the StompClientLib. Actually it is a problem with your backend side.

Here is What Is 422 Unprocessable Entity? article about the bad response. Please contact with your backend devs :) However, I'm also here to help, discuss your problem :) Have fun!

tomasimartin commented 4 years ago

hello @WrathChaos The same API is used in Kotlin (Android) and also in Java, on the same url. It works perfectly on both environments.

As I said in my previous message, I used another library to make websocket, and with the same url on that library it works.

That's why I'm focusing my research here. Has anyone ever encountered this problem?

WrathChaos commented 4 years ago

Actually there are some similar issues. Some related issues:

30

39

68

I know that it could work on Android however, iOS requires so many different things to do :( I believe that you need to research about it. Currently, it works with my example project & other recent projects. Have you ever tried with simple example project?

tomasimartin commented 4 years ago

@WrathChaos thanks for your quick feedback, we found the problem internally, it was a server side headers check. I hope I didn't disturb you too much. Well, hello from France. you can close this issue

WrathChaos commented 4 years ago

@tomasimartin I'm glad to hear that :) You're always welcome to come & brainstorm :) Please feel free to ask anything :)

tomasimartin commented 4 years ago

Hi @WrathChaos ,

we found out where the problem came from, when connecting there is an http call where we pass in the header

HEADER={{"host":["dev.myserver.fr"], "sec-websocket-protocol":["], "sec-websocket-key":["M6rALuZT9o3NRROXksQ==="], "sec-websocket-version":["13"], "upgrade":["websocket"], "origin":["http://dev.myserver.fr], "connection":["Upgrade"]}

but I don't find in this header the parameters I added during the connection:

let headerWS = ["deviceId":deviceToken]
socketClient.openSocketWithURLRequest(request: NSURLRequest(url: url as URL), delegate: self, connectionHeaders: headerWS)

it can come from the SocketRocket library, the initial call to the connection is made without taking into account the header.

I'm not good enough in objective-c to modify the lib myself.

Do you have an idea to pass my header ["deviceId":deviceToken], during the initial call

WrathChaos commented 4 years ago

Hey @tomasimartin, I'm gonna try it with modifying SocketRocket but I need some time to test it. ObjC is really a pain :D

tomasimartin commented 4 years ago

To get around my problem, I passed the header differently, like this


if let url = NSURL(string: hostws) {
            var deviceTokenManager = DeviceTokenManager()
            if let deviceToken = deviceTokenManager.retreiveToken() {
                let headerWS = ["deviceId":deviceToken]
                let request = NSMutableURLRequest(url: url as URL)
                request.setValue(deviceToken, forHTTPHeaderField: "deviceId")
                socketClient.openSocketWithURLRequest(request: request, delegate: self, connectionHeaders: headerWS)
            }
        }
bejeri4 commented 4 years ago

@tomasimartin Hello. I have got the same problem.

Error Domain=SRWebSocketErrorDomain Code=2132 "received bad response code from server 403" UserInfo={NSLocalizedDescription=received bad response code from server 403, HTTPResponseStatusCode=403}

As you said there is a server side problem (checking headers). Could you please give me more detailed information.

Thanks in advance!

tusharMistri commented 3 years ago

@WrathChaos , Can you please tell me what is reason behind this error.

Error Domain=SRWebSocketErrorDomain Code=2132 "received bad response code from server 403" UserInfo={NSLocalizedDescription=received bad response code from server 403, HTTPResponseStatusCode=403}

I have checked with backend server, and also verify with Android its also working fine, when i am trying to connect socket from iOS getting error.

I have tried all cases like as, URL : ws://{server_url}/ws/websocket URL : wss://{server_url}/ws/websocket URL : http://{server_url}/ws/websocket URL : https://{server_url}/ws/websocket

But i am not getting result. can you please explain where i am wrong and missing something from my end ?

Thanks in advance.

KaranMehta3984 commented 3 years ago

@tusharMistri Any luck?

AlexandreCassagne commented 10 months ago

why was this issue marked as closed?

I am getting the same issue.

EDIT: Never mind :D Unrelated issue. Cors settings are not shared between HTTP and Stomp/WS in Spring (my backend)

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/websocket")
                .setAllowedOriginPatterns("*");
    }