locked
My simple example is not working RRS feed

  • Question

  • User-966835673 posted

    Hi guys,

    I just tried to create a simple example but cannot understand why it is not working. Cannot find any documentation or working examples. Everything I have found is pointing to the old js framework.

    The error is: "Error during negotiation request., Status: 404, Status Text: Not Found".

    Could please anybody help me with that?

    https://1drv.ms/u/s!AjrOEl2CU1F9go9jt9NEWwbRXLcDOQ

    Thanks.

    Sunday, February 4, 2018 3:48 PM

All replies

  • User283571144 posted

    Hi ADM-IT,

    According to your application, I found you used the wrong SignalR client side js package.

    You need NPM (Node Package Manager). After you install NPM, you need to get the @aspnet/signalr-client package, after which, you need to get the signalr-client-1.0.0-alpha1-final.js file (the version may be different) from the node_modules\@aspnet\signalr-client\dist\browser folder and place it somewhere underneath the wwwroot folder, so that you can reference it from your pages.

    I suggest you could follow below steps to use Signalr in asp.net core.

    1.Add services.AddSignalR(); in ConfigureServices

    2.Register a chat hub, in the Configure method:

    app.UseSignalR(routes =>
    {
        routes.MapHub<ChatHub>("chat");
    });

    3.Create a hub

    public class ChatHub : Hub
    {
        public async Task Send(string message)
        {
            await this.Clients.All.InvokeAsync("Send", message);
        }
    }

    4.Create client script:

    <script src="~/signalr-client/signalr-client-1.0.0-alpha2-final.js"></script>
    <script src="~/signalr-client/signalr-clientES5-1.0.0-alpha2-final.js"></script>
    <script src="~/signalr-client/signalr-msgpackprotocol-1.0.0-alpha2-final.js"></script>
    <script src="~/signalr-client/signalr-msgpackprotocolES5-1.0.0-alpha2-final.js"></script>
    
    <script>
    
           var transportType = signalR.TransportType.WebSockets;
           //can also be ServerSentEvents or LongPolling
           var logger = new signalR.ConsoleLogger(signalR.LogLevel.Information);
           var chatHub = new signalR.HttpConnection(`http://${document.location.host}/chat`, { transport: transportType, logger: logger });
           var chatConnection = new signalR.HubConnection(chatHub, logger);
    
           chatConnection.onClosed = e => {
               console.log('connection closed');
           };
    
           chatConnection.on('Send', (message) => {
               console.log('received message');
           });
    
           chatConnection.start().catch(err => {
               console.log('connection error');
           });
    
           function send(message) {
               chatConnection.invoke('Send', message);
           }
    
    </script>

    Result:

    Best Regards,

    Brando

    Monday, February 5, 2018 5:43 AM