I tried to implement SignalR for ASP.NET Core as documented here: https://aspnetboilerplate.com/Pages/Documents/SignalR-AspNetCore-Integration
But I always get an error. Browser console output:
DEBUG:
Starting connection using WebSockets transport
Information: Normalizing '/signalr-myChatHub' to 'http://localhost:62114/signalr-myChatHub'
SCRIPT12008: WebSocket Error: Incorrect HTTP response. Status code 400, Bad Request
I'm using the latest version of all packages 3.6.2 and Abp.AspNetCore.SignalR 3.6.2-preview6.
Web.Host/Startup.cs:
#elif FEATURE_SIGNALR_ASPNETCORE
app.UseSignalR(routes =>
{
routes.MapHub<AbpCommonHub>("/signalr");
routes.MapHub<MyChatHub>("/signalr-myChatHub"); // Prefix with '/signalr'
});
#endif
Copy-pasted MyChatHub.cs:
public class MyChatHub : Hub, ITransientDependency
{
public IAbpSession AbpSession { get; set; }
public ILogger Logger { get; set; }
public MyChatHub()
{
AbpSession = NullAbpSession.Instance;
Logger = NullLogger.Instance;
}
public async Task SendMessage(string message)
{
await Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, message));
}
public override async Task OnConnectedAsync()
{
await base.OnConnectedAsync();
Logger.Debug("A client connected to MyChatHub: " + Context.ConnectionId);
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await base.OnDisconnectedAsync(exception);
Logger.Debug("A client disconnected from MyChatHub: " + Context.ConnectionId);
}
}
More-or-less copy-pasted View:
@using EMetall.Web.Startup
@section scripts
{
<environment names="Development">
<script src="~/lib/signalr-client/signalr.min.js"></script>
<script src="~/lib/abp-web-resources/Abp/Framework/scripts/libs/abp.signalr-client.js"></script>
</environment>
<script type="text/javascript">
$(function () {
var chatHub = null;
abp.signalr.startConnection('/signalr-myChatHub', function (connection) {
chatHub = connection; // Save a reference to the hub
connection.on('getMessage', function (message) { // Register for incoming messages
console.log('received message: ' + message);
});
}).then(function (connection) {
console.log('Connected to myChatHub server!');
abp.event.trigger('myChatHub.connected');
});
abp.event.on('myChatHub.connected', function() { // Register for connect event
chatHub.invoke('sendMessage', "Hi everybody, I'm connected to the chat!"); // Send a message to the server
});
});
</script>
}
As of Version 4.2 it works out of the box.
You just have to download it and then create the chathup, register the route and call it in the view and it works. Great.
See for example for the chathup, route and view: https://aspnetboilerplate.com/Pages/Documents/SignalR-AspNetCore-Integration