Code-Sharp / WampSharp

A C# implementation of WAMP (The Web Application Messaging Protocol)
http://wampsharp.net
Other
385 stars 84 forks source link

Is it possible to register a js function with an object param. #320

Closed zhusheping closed 4 years ago

zhusheping commented 4 years ago

Like this:

        var connection = new autobahn.Connection({ url: 'ws://127.0.0.1:8804/', realm: 'realm' });
        connection.onopen = function (session) {
            function onevent(args) {
                console.log("Event:", args[0]);
            }
            function square(person) {   //Is it possible?
                return person.Age * person.Age;
            }
            session.register('com.math.quare.3', square).then(
                function (registration) {
                    console.log("Procedure registered:", registration.id);
                },
                function (error) {
                    console.log("Registration failed:", error);
                }
            );
        };
        connection.open();

the model:

   public class Person
    {
        public int Age { get; set; }
        public string Name { get; set; }
    }
    public interface IArgumentsService
    {
        [WampProcedure("com.math.quare.3")]
        Task<string> SquareAsync(Person p);
    }

I'm trying to do this.and i got a exception:"Error converting value {null} to type 'System.Int32'. Path ''.”". Zip. 1.Run project wampHost. 2.Open index.html under project wampCaller to register func. 3.Run project wampCaller to call it. 4.Exception throwed.

darkl commented 4 years ago

Your AutobahnJS code is wrong, please read their documentation. A callee receives its arguments as two parameters, an array of positioned arguments and a dictionary of keyword arguments.

The corrected code should look like this:

function square(arguments) {
    person = arguments[0];
    return person.Age * person.Age;
}

To avoid these kind of errors, I strongly encourage you to use TypeScript with the AutobahnJS definitions I contributed to the DefinitelyTyped project.

darkl commented 4 years ago

Also please refrain from uploading code to OneDrive/Dropbox/Google Drive, as it makes my antivirus suspicious. Upload your code as a GitHub gist (minimal as possible) or as a GitHub repository (if you have too much code).