Icidis / Blazor.MQTT.Chat

Blazor chat application via MQTT using RabbitMQ and Paho
7 stars 5 forks source link

Net6 JsInterop problems #1

Open autoprog opened 11 months ago

autoprog commented 11 months ago

hi, the design as this works ok. I create a new project, with .Net6 client or server mode, gives errors in JsInterop, I tried to solve it but it's complicated. I would like to receive help to register this functions to inject javacript. I'm trying to do it like this: protected override async Task OnInitializedAsync() protected async override Task OnAfterRenderAsync(bool firstRender).

as I saw some examples, but without success until then.

Blazor, at first I want a PWA App, but it can be MVC to resolve.

thanks, Carlos.

autoprog commented 11 months ago

hi, try register functions

protected async override Task OnAfterRenderAsync(bool firstRender) {

    if (firstRender)
    {

        Console.WriteLine("Load modules ok" );
        dotNetObjectReference = DotNetObjectReference.Create(this);
        await jSRuntime.InvokeVoidAsync("mqttFunctions.createClient", dotNetObjectReference);
        dotNetObjectReference = DotNetObjectReference.Create(this);
        await jSRuntime.InvokeVoidAsync("mqttFunctions.connect", dotNetObjectReference);
        dotNetObjectReference = DotNetObjectReference.Create(this);
        await jSRuntime.InvokeVoidAsync("mqttFunctions.disconnect", dotNetObjectReference);
        dotNetObjectReference = DotNetObjectReference.Create(this);
        await jSRuntime.InvokeVoidAsync("mqttFunctions.publish", dotNetObjectReference);
    }
}

///----------log

fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111] Unhandled exception in circuit 'ztfUHAJzUGP7Kr_ORH_dhB7rBTV6qalNn4z4-mrQQgY'. Microsoft.JSInterop.JSException: Paho is not defined ReferenceError: Paho is not defined at Object.createClient (http://localhost:5155/scripts/paho-mqtt-interop.js:16:9) at http://localhost:5155/_framework/blazor.server.js:1:3501 at new Promise () at kt.beginInvokeJSFromDotNet (http://localhost:5155/_framework/blazor.server.js:1:3475) at http://localhost:5155/_framework/blazor.server.js:1:72077 at Array.forEach () at kt._invokeClientMethod (http://localhost:5155/_framework/blazor.server.js:1:72063) at kt._processIncomingData (http://localhost:5155/_framework/blazor.server.js:1:70105) at connection.onreceive (http://localhost:5155/_framework/blazor.server.js:1:64508) at o.onmessage (http://localhost:5155/_framework/blazor.server.js:1:48842) at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, Object[] args) at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime jsRuntime, String identifier, Object[] args) at Apps_one.Pages.Index.OnAfterRenderAsync(Boolean firstRender) in C:\Users\Carlos\source\repos\Apps-one\Pages\Index.razor:line 79 at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)