In C# i would hook many handlers to an event like so:
event += firstEventHandler;
event += secondEventHandler;
but JavaScript with SignalR we write:
$.connection.someHubName.client.someEventName = function (item) {
console.log("someMessage", item);
};
I have created a wrapper that looks a little like this:
var signalRClient = {
start: function (callback) {
$.connection.hub.url = ajaxHttp + "/signalr";
$.connection.hub.logging = true;
if ($.connection.hub && $.connection.hub.state === $.signalR.connectionState.disconnected) {
$.connection.hub.start({ jsonp: true, withCredentials: true, transport: ['webSockets', 'longPolling'] }).done(callback);
} else {
callback();
}
},
connection: $.connection
}
That i can call in this way:
signalRClient.connection.somehub.client.someEvent = function (item) {
console.log("someMessage", item);
};
signalRClient.start(function () {
signalRClient.connection.somehub.server.subscribe(someId);
});
but i if i run the signalRClient in more than one view (on the same rendered page) it can only assign one function to one event. I want to be able to assign many functions to one event.
I have read: connect to signalr hub and SignalR client with multiple connections ++
But i don't think it's a good idea to create many connections to the same hub for this purpose. I would also try to avoid creating my SignalR code in a layout and make all views dependent on this.
Is there a simple solution available so that i can hook on many events handlers to the $.connection.somehub.client.event
?
Only one event handler can be assigned for every event in an element. If needed the handler can be replaced by assigning another function to the same property. Below we show how to set a simple greet() function for the click event using the onclick property.
In the default mode, the app server creates five server connections with Azure SignalR Service. The app server uses the Azure SignalR Service SDK by default. In the following performance test results, server connections are increased to 15 (or more for broadcasting and sending a message to a big group).
The default keepalive timeout period is currently 20 seconds. If your client code tries to call a Hub method while SignalR is in reconnecting mode, SignalR will try to send the command.
SignalR Hubs are a way to logically group connections as documented. Take an example of a chat application. A group of users could be part of the same hub which allows for sending messages between users in the group. The hubName here can be any string which is used to scope messages being sent between clients.
After trying many different things i managed to use the .on( command properly.. it is done like this:
signalRClient.connection.somehub.on("someevent", function(item) {
console.log(item);
});
signalRClient.connection.somehub.on("someevent", function (item) {
console.log(item);
});
This will provide two lines in the console for each event.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With