SignalR self host: Connection must be started before data can be sent. Call .start() before .send()

StackOverflow https://stackoverflow.com/questions/23346541

  •  11-07-2023
  •  | 
  •  

Question

I know it's yet another post relating to this problem :)

I'm trying to rewrite my project to make use of SignalR Self-Host library (as per this tutorial), but can't get passed this error now.

I initialise the connection as follows:

$.connection.hub.url = "http://localhost:8182/signalr";
$.connection.hub.logging = true;
var connection = $.connection.hub.start();

console.log(connection.state());

connection.done(function () {
    console.log(connection.state());
    [...] // initialising viewmodels in here...
});

When the first viewmodel is initialised I get the error as per the thread subject:

[17:29:14 GMT+0100 (GMT Daylight Time)] SignalR: Auto detected cross domain url. jquery.signalR-2.0.3.js:76
[17:29:14 GMT+0100 (GMT Daylight Time)] SignalR: Negotiating with 'http://localhost:8182/signalr/negotiate?clientProtocol=1.3'. jquery.signalR-2.0.3.js:76
pending main.js:218
[17:29:14 GMT+0100 (GMT Daylight Time)] SignalR: Attempting to connect to SSE endpoint 'http://localhost:8182/signalr/connect?transport=serverSentEvents&connection…pdQlpKjRBpUhB0PY4itVfHasSmixQAAAB0HA7hA9gYDflrhGockG%2FZR55tLg%3D%3D&tid=1'. jquery.signalR-2.0.3.js:76
[17:29:14 GMT+0100 (GMT Daylight Time)] SignalR: EventSource connected. jquery.signalR-2.0.3.js:76
[17:29:14 GMT+0100 (GMT Daylight Time)] SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332 and a connection lost timeout of 20000. jquery.signalR-2.0.3.js:76
resolved main.js:220
Object {getAccountActions: function, getIncidentActionCount: function, getIncidentActions: function, refreshAccountActions: function, refreshActions: function…}
 IncidentAction.js:12
Uncaught Error: SignalR: Connection must be started before data can be sent. Call .start() before .send() 

I console.log connection state after connection.done and is clearly showing as resolved.

Also in the first viewmodel I initialise, I console.log connection.server and all my server methods are visible there, as per the snippet above.

Server side code:

class Program
    {
        static void Main(string[] args)
        {
            string url = "http://localhost:8182";
            using (WebApp.Start(url))
            {
                Console.WriteLine("Server running on {0}", url);
                Console.ReadLine();
            }
        }
    }

Any suggestions, why I am getting this error even though the connection is 'resolved'?

Edit: Originally I moved my Owin Startup class on the server to a different project within my solution and I assume this caused the problem. When I put it in my signalR self host project, the error is gone

Was it helpful?

Solution

The connection state should be connected

Try it like this :

 $.connection.hub.start().done(function () {
         console.log(connection.state());
     })  .fail(function (error) {
         console.log('Invocation of start failed. Error:'+ error)
     });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top