microsoft / BotFramework-WebChat

A highly-customizable web-based client for Azure Bot Services.
https://www.botframework.com/
MIT License
1.58k stars 1.53k forks source link

Error during WebSocket handshake: Unexpected response code: 403 #4071

Open GLDev21 opened 2 years ago

GLDev21 commented 2 years ago

Using directLine or Webchat with my bot

After 5 minutes of using the bot i get a Websocket error.

authentication.service.ts:85 WebSocket connection to 'wss://directline.botframework.com/v3/directline/conversations/49zS5PwGN3TFWlfLAgKoKz-us/stream?watermark=-&t=ew0KICAiYWxnIjogIlJTMjU2IiwNCiAgImtpZCI6ICJ2cG4yYXhuRjFSU3NHUXg5aXd0WGEwZTNjRWciLA0KICAieDV0IjogInZwbjJheG5GMVJTc0dReDlpd3RYYTBlM2NFZyIsDQogICJ0eXAiOiAiSldUIg0KfQ.ew0KICAiYm90IjogIlBNRS1Cb3QtRCIsDQogICJzaXRlIjogIjl4TEU5aTVncEx3IiwNCiAgImNvbnYiOiAiNDl6UzVQd0dOM1RGV2xmTEFnS29Lei11cyIsDQogICJuYmYiOiAxNjMzNjk1NDAwLA0KICAiZXhwIjogMTYzMzY5NTQ2MCwNCiAgImlzcyI6ICJodHRwczovL2RpcmVjdGxpbmUuYm90ZnJhbWV3b3JrLmNvbS8iLA0KICAiYXVkIjogImh0dHBzOi8vZGlyZWN0bGluZS5ib3RmcmFtZXdvcmsuY29tLyINCn0.ScI-lSR2R1mQ3yUda7sYYJCU--hy6m0yLAgWgF0sEQ80-o6r90tvkBIJ8olR4mPJZxBbRTsnUnREXr-Bc61LM6OsjDGqa0kkJsA8jlF5Sw00wBrXND2PUGsWcauBppd6FRHmp5qhrivQQ9IpgnqRevkG8rVyIKUOi_-lbQz1AwWWHxxbpqfrRWKFvjyZplhDDL81i7dWYXQZRQn24LyWRgRVppdQcJHIFP5u5Zhcb37rp-6ElO7cRJULE6DCx73lg1rHjpeBzVDPKzAbSZWu8jt628qgIGYNVPRHqiCS05NXnGBkO-RJY0dwvUZ1nEVFTWsU79MEm6QTS4mVyZ9dHw' failed: Error during WebSocket handshake: Unexpected response code: 403

If I let the chat window open everything is fine. When i close the chat windows and then reconnect to the conversation the websocket error appear and the history isn't comming back. The chat still work but if i close it again the history is not saved anymore to the conversation.

I used postman to lisen to the websocket stream and i can see everything pass. When I try to connect to a stream after 5 minutes(after the websocket error), i got this message

Error: Unexpected server response: 403 Handshake Details Request URL: https://directline.botframework.com/v3/directline/conversations/1xmoTM750ySKSoi8JvxNMV-6/stream?watermark=-&t=ew0KICAiYWxnIjogIlJTMjU2IiwNCiAgImtpZCI6ICJ2cG4yYXhuRjFSU3NHUXg5aXd0WGEwZTNjRWciLA0KICAieDV0IjogInZwbjJheG5GMVJTc0dReDlpd3RYYTBlM2NFZyIsDQogICJ0eXAiOiAiSldUIg0KfQ.ew0KICAiYm90IjogIlBNRS1Cb3QtRCIsDQogICJzaXRlIjogIjl4TEU5aTVncEx3IiwNCiAgImNvbnYiOiAiMXhtb1RNNzUweVNLU29pOEp2eE5NVi02IiwNCiAgIm5iZiI6IDE2MzMzNTk5OTUsDQogICJleHAiOiAxNjMzMzYwMDU1LA0KICAiaXNzIjogImh0dHBzOi8vZGlyZWN0bGluZS5ib3RmcmFtZXdvcmsuY29tLyIsDQogICJhdWQiOiAiaHR0cHM6Ly9kaXJlY3RsaW5lLmJvdGZyYW1ld29yay5jb20vIg0KfQ.tNDu_5s4FhPHxhKNjsaXB6t5OVHZ1z6jMto9olBXIVmt4myAP_Gw--RsS3B6cuR5roL9n5fPPo_gUVWye8SsYG9Y28yKheVAnFto6XlD2e5Ew-llJr4fXmpQW9sXDcO_lp4oLsWvWogj33xH7ua1smP6oEZ9ZvfKl1XrTW2E0fpplPKzZPJsal3iBKVseRIRisPIQqcZSD-Dw9dmJWfcy-MWW1j78pumDCk-p4p6Dpts4yGH-v_oguaVcB-1-6bnSuwxljIbcFGtHc6jRE5acriCrCWFvcER99BoHHxvTHOfHBDmBFrjLPx2nsxV7hOaLpAsx8N4p8XkRaIGM_owDw Request Method: GET Status Code: 403 Forbidden Request Headers Sec-WebSocket-Version: 13 Sec-WebSocket-Key: 07TEu7JXBDayOkiMgA/l8g== Connection: Upgrade Upgrade: websocket Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits Host: directline.botframework.com Response Headers Cache-Control: no-cache Pragma: no-cache Content-Length: 88 Content-Type: application/json; charset=utf-8 Expires: -1 X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block Strict-Transport-Security: max-age=31536000 ARR-Disable-Session-Affinity: true Date: Mon, 08 Oct 2021 15:13:14 GMT

I tried to make a loop to send a message each 60 seconds on my website with a secondary direct line connection to the same credentials. I wanted to find out if it's en idle problem and it's not.

      this.mySubscription= interval(environment.webSocketPingDelay).subscribe((x =>{
            let directLine = window.WebChat.createDirectLine({
                token: this.storageService.token,
                webSocket: environment.payload.webSocket,
                conversationId: this.storageService.conversationId,
                streamUrl: this.storageService.streamUrl
            });

        directLine.postActivity({
            from: { id: Authentication.user, name: "USER_NAME" },
            name: "keep_alive",
            type: "message",
            Text: "Keep"
        }).subscribe();
      }));

Since the web socket address indicate that the websocket is hosted on azure. "wss://directline.botframework.com/v3/directline/conversations/***"

On my azure channel i see no option to configure websocket.

On my webapi i used this configuration in my startup.cs

var webSocketOptions = new WebSocketOptions() { KeepAliveInterval = TimeSpan.FromSeconds(120), };

app.UseWebSockets(webSocketOptions);

How can i configure it to last more than 5 minutes ?

EricDahlvang commented 2 years ago

Hi @GLDev21

It should be possible to reconnect to an existing conversation https://docs.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-direct-line-3-0-reconnect-to-conversation . However, reconnecting is not a matter of using the same wss, but calling the reconnect api.

However, based on your description there might be some misunderstanding. The websocket from WebChat is actually connected to just the DirectLine service, there is not a socket all the way to the bot (so the webapi socket connection timeout is not relavent).

@compulim please review

GLDev21 commented 2 years ago

So your telling me that the streamUrl lifetime is only 5 minutes. My token and conversationId will remain the same but the streamUrl will have to be manually change? Do I have to call the reconnect before the 5 minutes is reach or can i do it on my next usage. How much delay I have to be able to call reconnect when my original stream is giving me websocket handshake error?

GLDev21 commented 2 years ago

Any chance you found the answer to my questions?

msomanathan commented 2 years ago

@compulim - Could you please share any updates on this issue?

johnataylor commented 2 years ago

@compulim any update on this?

GLDev21 commented 2 years ago

No, we are still having the issue, no answer to my 12oct question, No news from @compulim

Jesse-dw commented 2 years ago

Your streamUrl lifetime does not expire as far as I know except when you lose connection, what happens e.g. with a page refresh. In this case you have to call the reconnect api to retrieve your chat history.

Your token lifetime remains valid for 30 minutes, before this you will have to call the refresh token api in order to renew your token.