3

I am working on a Javascript multiplayer game and I need to send a Javascript object from one client to another client using signalR. Till now I am sending client to client data by string or array.
But I don't know how to receive Javascript object in server for sending that object to another client.

var MyInfo = {
    UserName: loginUserName,
    userid: logInUserId,
    getinfo: function() {
        return this.UserName + ' ' + this.userid;
    }
}

Which data type shall I use to receive that Javascript data in my hub.
I am working on C# .NET MVC.

long.luc
  • 1,191
  • 1
  • 10
  • 30

3 Answers3

2

I got the answer of my problem...
C# language provides automatically conversion of Javascript object to Object data type. Thus I send the Javascript object to server and then receive that object in Object datatype. After that I send that object to destination client, as follow:

var MyInfo = {
    UserName: loginUserName,
    userid: logInUserId,
    getinfo: function() {
        return this.UserName + ' ' + this.userid;
    }
};

var MyInfo2 = {
    UserName: "",
    userid: "",
    getinfo: function() {
        return this.UserName + ' ' + this.userid;
    }
};

var chessR = $.connection.gameHub;
var myConnectionID;
chessR.client.testObject = function(temp) {
    MyInfo2.UserName = temp.UserName;
    MyInfo2.userid = temp.userid;
    alert(MyInfo2.getinfo());
}
$.connection.hub.start().done(function() {
    chessR.server.testObject(MyInfo);
});

On signalR hub I write:

public class GameHub : Hub
{
    public void testObject(Object MyInfo)
    {
        Clients.Caller.testObject(MyInfo);
    }
}

Now the problem solved.

long.luc
  • 1,191
  • 1
  • 10
  • 30
0

I think the easy way to go would be creating a MyInfoModel on the server side containing the same properties of the JS model and simply pass it on the server method.

Normally, SignalR and ASP.NET should handle the serialization of your data, letting you to send and recieve complex objects. (Please note that I have not tested it, it is just an educated guess). Since you can easily send complex objects from the server to the client, I see no reason why you cannot send them from the client to the server.

If the first approach (creating the model on the server and make the server method accept a model) doesn't work, you can serialize the object, send it as a string and deserialize it on the hub (you still need the model to deserialize).

If neither of these work, leave a message and when I get to a computer with VS I will test them and share the results.

Hope this helps! Good luck!

radu-matei
  • 3,469
  • 1
  • 29
  • 47
0

Your solution is good for sending the object between JS clients, but if you want to use the object on the C# base Server or get some typing (usually ideal), then you can create a C# model that matches the JS object.

And depending on the signalr Json serialize casing options you use, you can use attributes to specify how the C# object should be de/serialized:

using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

    public class MyInfoModel
    {
        [Required]
        [JsonPropertyName("userId")]
        public string UserId { get; set; }

        [Required]
        [JsonPropertyName("userName")]
        public string UserName { get; set; }
    }

JS client side (in typescript)

type UserInfo = {
  userName: string;
  userId: string;
};

const myInfo: UserInfo = {
  userName: loginUserName,
  userId: logInUserId,
};

await $.hubConnection.send("HubMethodWithTypedParam", myInfo);

Now the hub method can be declared with a typed parameter like this:

public class GameHub : Hub
{
    public void HubMethodWithTypedParam(MyInfoModel myInfo)
    {
        ...
    }
}
  • Yes, I think your solution should work too. Thanks for reply maybe this would help someone . –  Mar 04 '22 at 13:36