takeyourcode
takeyourcode

Reputation: 445

Blazor server side problem share javascript code

I'm developing my project with Blazor Server-side. While I develop, I used javascript code to implement things that hard to implement by C#.

However, I'm facing something weird situation. (I guess it is problem for javascript)

Suppose there are 2 users(A, B). When 'A' user do some action that call javascript code, if 'B' user into same page, 'A' users action affects to 'B' user.

I implemented web page that have 3d scene with threejs. As I explained above, when User 'A' move some object with mouse event(mousemove, mousedown..), if User 'B' accesses the same page, 3d objects of B are moved to the location where User 'A' moved. Originally, when user access to web page I developed, 3d objects's position should be 0,0,0.

My Guess

I'm guessing the javascript would be problem, but if you have any other opinions, would you please share?

Edited

I've solved this problem using DotNetObjectReference.Create(this);

C#

protected override async Task OnAfterRenderAsync(bool firstRender)
{
    if (firstRender)
    {
        //send created instance to javascript
        var dotNetObjRef = DotNetObjectReference.Create(this);
        await JSRuntime.InvokeVoidAsync("SetObjectRef", dotNetObjRef);
    }
    await base.OnAfterRenderAsync(firstRender);
}

[JSInvokable]
public async Task enableSomething(bool bEnable)
{
   var something = bEnable;
}

//== before edit
//[JSInvokable]
//public static async Task enableSomethingStatic(bool bEnable)
//{   
//    var something = bEnable;
//}

Javascript

var objectRef;
function SetObjectRef(ref) {
    objectRef = ref;
}

//call c# function
objectRef.invokeMethodAsync("enableSomething", true);

It was problem of 'static' method as I guessed. If you declare C# method called from javascript as 'static' and this method changes something of UI variable, this method can affect another users.

So I create instance of current page and send it javascript and when I need to call C# methods from javascript, I call methods using created instance.

Is there any problem or issue, please share it. Sorry for my bad English.

Upvotes: 1

Views: 738

Answers (1)

Bennyboy1973
Bennyboy1973

Reputation: 4256

JavaScript runs client side only. I don't see how two windows, let alone two users, would share data.

Almost for sure, the problem is that you are injecting a singleton service-- which means the server will use one instance for all users.

If so, you have two choices:

(1) add logic to your singleton service to incorporate users. (For example, a dictionary with UserID/Property name for key, and a column for Value)

(2) go to Startup.cs and change the suspect singleton service to .AddScoped(), which will create a new instance for each user.

For right now, I think the latter solution will solve your problem immediately. However, don't underestimate the value of Singletons-- they'll be very useful for other things.

Upvotes: 2

Related Questions