Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR: getting error: WebSocket closed

I am working on project of Ionic with angular and AspNet with SignalR that have chat module. I use SignalR for Chat.It's working smoothly but some time i am getting error as per below screen shot and because of that it's get stop working at all. I have hosted my service on IIS and creating proxy and communicating with client and server. Here is sample

    (function () {
        angular
          .module('app')
          .factory('SignalRFactory', SignalRFactory);
        SignalRFactory.$inject = ['$rootScope', 'Hub', 'ionicToast'];
        function SignalRFactory($rootScope, Hub, ionicToast) {
            var signalRLocal = this;

            var serverURL = 'https://serivcerURL.com/signalr';

            //Hub setup
            var hub = new Hub('CommunicationHub', {
                rootPath: serverURL,
                listeners: {
                    'send': function (data) {
                        console.log("send " + data);

                    }

                },


                errorHandler: function (error) {
                //Here i am getting that websocket closed error
                    console.error(error); 

                }
            });


            signalRLocal.Connect = function (user) {
                console.log("SignalR Connecting as :" + user.UserName);
                hub.invoke('connect', user);
            };


            return signalRLocal;
        }
    })();

enter image description here

I have hosted service on IIS. I search for the solution and find something like this link

I also try with above link solution by using "long Polling" as per below

Hub.connection.start({ transport: 'longPolling' });

But i don't want to use "long Polling" at all. So can someone help me to figure out this issue without use of 'long Polling'. Can someone tell me what configuration i have to do at client side or at IIS level.

like image 596
Sagar Hirapara Avatar asked Oct 30 '22 04:10

Sagar Hirapara


1 Answers

As we said in comments, SignalR client will try to reconnect after the connection is lost. Besides, many factors (such as physical network interruption, client browser failure, server offline etc) can cause the connection lost, this article explains some disconnection scenarios, you can refer to it and find the possible causes of the issue.

Besides, as I mentioned in comment, you can call the Start method from your Closed event handler (disconnected event handler on JavaScript clients) to start a new connection to make client automatically re-establish a connection after it has been lost.

edit:

The connection to ws://localhost:3156/signalr/signalr/connect?transport=webSoc‌​kets&clientProtocol=‌​1.5&connectionToken=‌​g8vpRv9ncVDjPIYB9UuE‌​pAAILEaOcTMTG9p46IA2‌​4 was interrupted while the page was loading.

Under "Client disconnection scenarios" section in the article, you can find:

In a browser client, the SignalR client code that maintains a SignalR connection runs in the JavaScript context of a web page. That's why the SignalR connection has to end when you navigate from one page to another, and that's why you have multiple connections with multiple connection IDs if you connect from multiple browser windows or tabs. When the user closes a browser window or tab, or navigates to a new page or refreshes the page, the SignalR connection immediately ends because SignalR client code handles that browser event for you and calls the Stop method.

like image 140
Fei Han Avatar answered Nov 15 '22 06:11

Fei Han