1

I have signalr server running. That keeps sending updated information.

I have following code in index.html within tag

$.connection.hub.url = "http://localhost:8080/signalr";
// Declare a proxy to reference the hub.
var chat = $.connection.myHub;
// Create a function that the hub can call to broadcast messages.
chat.client.addMessage = function (name, message) {
// Html encode display name and message.
console.log ("chat.client.addMessage.index");
};

And same is added in component ts file

 ngOnInit () {
    jQuery.connection.hub.url = "http://localhost:8080/signalr";
    // Declare a proxy to reference the hub.
    var chat = jQuery.connection.myHub;
    // Create a function that the hub can call to broadcast messages.
    chat.client.addMessage = function (name, message) {
    // Html encode display name and message.
      console.log ("chat.client.addMessage.index");      ""
    };
  }

The addMessage is called from SignalR Hub server. And is received by addMessage registered in index.html but not by function registered in component ts.

On debug, I could find that function has got registered in both places. I tried registering different functions and server calling both functions. But in either cases the function registered in component/ type script is not called.

any help will be thankful

1
  • Could you please the below answer as the accepted solution so it helps others? Commented Nov 16, 2017 at 4:22

1 Answer 1

1

I had similar issues getting SigR to work but managed to get it to work in the end. Depending on how you are import jQuery for the connection functions I got it to work like this...

this.connection = $.hubConnection("Url");
this.proxy = this.connection.createHubProxy("HubName");

this.proxy.on('someFunctionName', (data: any) => {
     console.log(data);
});
Sign up to request clarification or add additional context in comments.

9 Comments

var connection = jQuery.connection('localhost:8080/signalr'); var proxy = connection.createHubProxy("myHub"); It fails with message during run time EXCEPTION: connection.createHubProxy is not a function
It depends how you are bringing in jquery like l mentioned. Add this to the top of the file... declare var $: any;
Now I have following code: after declare $ : any ngOnInit () { $.connection.hub.url = "localhost:8080/signalr"; var chat = jQuery.connection.myHub; $.connection.hub.start().done(function () { }); chat.client.addMessage = function (name, message) { }; } My web server is running in port 3000 and singnalr is running on 8080 I am getting following error. But I do not see any of this error if I include the same code in index.html
(index):1 XMLHttpRequest cannot load extsgo.com/api/tracker/…. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:3000' is therefore not allowed access. The response had HTTP status code 404. zone.js:1382 GET extsgo.com/api/tracker?tracking_id=47 net::ERR_CONNECTION_CLOSED
ok so this error went if I move the call back method before calling start. So now I do not see any error. But now when server sends message. addMessage function is not called in client.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.