Bit of a strange one this, I have signalR installed and I am getting all the network messages (by examining them with chrome dev tools), even the 'ping / pong' messages keeping the connection alive, but I cant for the life of me get the events to fire in jquery. I have tried 2 different ways:
var chat = $.connection.chatHub;
$.connection.hub.logging = true;
$.connection.hub.error(function (error) {
console.log('SignalR error:', error)
chat.client.ReceiveMessage = function (name, message, count) {
console.log("proto message");
console.log(count + " " + name + ": " + message);
}
chat.on("ReceiveMessage", (user, message, count) => {
console.log(count);
var encodedUser = $("<div />").text(user).html();
var encodedMessage = $("<div />").text(message).html();
var li = "<li><strong>" + encodedUser + "</strong>: " + encodedMessage + "</li>";
$("#messages").append(li);
});
$.connection.hub.start({ transport: ['webSockets', 'longPolling'] }).done(function () {
console.log("connected");
console.log($.connection.hub);
});
I put another parameter onto the MessageReceived function to give a message count, just to see if I was actually communicating.
Here is my hub:
Module ChatCount
Public count As Integer = 0
End Module
Public Class ChatHub : Inherits Hub
Public Async Function SendMessage(ByVal user As String, ByVal message As String) As Task
If message = "reset" Then count = 0
Await Clients.All.SendAsync("ReceiveMessage", user, message, count.ToString)
ChatCount.count += 1
End Function
End Class
And my Startup Class (works with or without the Configuration):
<Assembly: OwinStartup(GetType(Startup))>
Public Class Startup
Public Sub Configuration(ByVal app As IAppBuilder)
Dim h As New HubConfiguration With {
.EnableDetailedErrors = True,
.EnableJavaScriptProxies = True
}
app.MapSignalR()
'app.MapSignalR(h)
End Sub
End Class
Finally... Assemblies/Versions:
signalR.AspNet.Core 2.4.3
jquery-3.7.1.min.js
jquery.signalr-2.3.0.min.js
It's been frustrating me for over a week, so I thought somebody else might have had the same issue. I get the connected message in the console, but nothing else. Looking at the transport logs I get this message back after every sent message, and if I put 'reset' as the message, it zeros the count as it should.
{
"C": "d-9728E6BF-B,2A|c,0|d,1",
"M": [
{
"H": "ChatHub",
"M": "SendAsync",
"A": [
"ReceiveMessage",
"psychuk",
"testing",
"41"
]
}
]
}
I am thinking it's most definitely a JS/JQ issue, those events are just not hooking as they should, although I cant find a way to check this, I am 99% sure signalR is working as it should. Any help would be appreciated (even if someone could just check the returned message format for me so I know it's correctly typed).