Send request and wait for response ?

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

genar
Posts: 137
Joined: 13 Jul 2017, 11:49

Send request and wait for response ?

Postby genar » 10 Aug 2021, 11:42

I have a strong web background and probably found something very important missing in smartfoxserver ( or i just havent found it yet ).

Lets say our client opens up his fancy profile page. We need to fetch the profile data from the server to paste the informations into our client ui.
In javascript frameworks we would simply run a asynchron post/get operation to wait for the servers data... this looks like this.

Code: Select all

// Make a request for a user with a given ID
axios.get('/user?ID=12345').then(
   function (response) {
    // Unwrap packet and update the ui.
    }
)


Simple right ? We just communicate with a certain url, pass some data... wait till we receive something and update the ui.
In SFSX however we need to send a extension request with a optional packet. The server reponse however is received somewhere else...

Code: Select all

void SomeMethod() {
    sfs.addEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);

    // Send two integers to the Zone extension and get their sum in return
    ISFSObject params = SFSObject.NewInstance();
    sfs.Send( new ExtensionRequest("add", params) );
}

void OnExtensionResponse(BaseEvent evt) {
    String cmd = (String)evt.Params["cmd"];
    if (cmd == "add") {
        // Update ui
    }
}


This is actually pretty bad for most UI development which relies on sfsx... could you please add some sort of async extension ? Like this ?

Code: Select all

void SomeMethod() {

    // Send two integers to the Zone extension and get their sum in return
    ISFSObject params = SFSObject.NewInstance();
    sfs.Send( new ExtensionRequest("add", params)).then((reponse) => { // Update UI })
}


This does not need to replace the already existing workflow. But it would ease the development for many, many different tasks alot !
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Send request and wait for response ?

Postby Lapo » 10 Aug 2021, 15:06

Hi,
This is actually pretty bad for most UI development which relies on sfsx... could you please add some sort of async extension ? Like this ?

It's strange that you ask for asynchronous support, because communication in SFS2X is 100% asynchronous :)
You send a request and eventually you'll get a response, via an event handler. The definition of asynchronous messages.

What you seem to be asking is some kind of client-side helper that allows you to chain a request with a response handler. However, since every request has its specialized event handler it makes little sense to do that. The main reason being that you need to tell the API which function is going to handle the response for a specific request. (e.g. Extension requests).

Also difference between the two approaches is mostly syntactic, but it's essentially the same in terms of coding. In your example you define a response handler via a then() function that takes an anonymous function, whereas in SFS2X you have to register a function (same operation) to a response type (i.e. which event triggers which handler).

At the end of the day you're always writing a function with the code that handles the response. The difference is where (in your code) you do it.

There's not much we can do about this because SFS2X is a game-dev tool, not a web-dev API.
Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Send request and wait for response ?

Postby Lapo » 10 Aug 2021, 15:12

To add a further comment to your coding proposal:
sfs.Send( new ExtensionRequest("add", params)).then((reponse) => { // Update UI })


This is not possible because you need to tie one event handler to it's response type.
To change this it would require a profound change in the whole architecture of the API because each response must be uniquely tied to it's original request (via some kind of ID) which in turn would require changes to the protocol, adding extra data to they payload (which is bad for real time games) and a redesign of the client API.

This isn't something we have plans to implement, sorry.
Lapo

--

gotoAndPlay()

...addicted to flash games
genar
Posts: 137
Joined: 13 Jul 2017, 11:49

Re: Send request and wait for response ?

Postby genar » 11 Aug 2021, 20:15

Lapo wrote:To add a further comment to your coding proposal:
sfs.Send( new ExtensionRequest("add", params)).then((reponse) => { // Update UI })


This is not possible because you need to tie one event handler to it's response type.
To change this it would require a profound change in the whole architecture of the API because each response must be uniquely tied to it's original request (via some kind of ID) which in turn would require changes to the protocol, adding extra data to they payload (which is bad for real time games) and a redesign of the client API.

This isn't something we have plans to implement, sorry.


Thanks a lot for the fast response ! I can fully understand that its not possible to implement this in the current architecture... but probably this is not needed at all. What about some sort of simulation on top of the current mechanics ? Let me explain...

Currently the workflow is the following...

Code: Select all

void SomeMethod() {

    // Register event handler
    sfs.addEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);

    // Send two integers to the Zone extension and get their sum in return
    ISFSObject params = SFSObject.NewInstance();
    sfs.Send( new ExtensionRequest("add", params) );
}

void OnExtensionResponse(BaseEvent evt) {
    String cmd = (String)evt.Params["cmd"];
    if (cmd == "add") {
        // stuff to do
    }


So we register a method to receive a repsonse and then send a request to receive a response... there could be some sort of build in extension method simulating a .then by doing this...

Code: Select all

/// <summary>
    /// An extension which contains methods for <see cref="Smartfox"/>
    /// </summary>
    public static class SmartfoxExtensions {
       
        public static void Send(this SmartFox sfsx, string cmd, ExtensionRequest request, Action<ISFSObject> responseCallback) {

            // Create delegate which wrapps the callback
            EventListenerDelegate listener = null;
            listener = evt => {
               
                // Only when the response contains our previous cmd, execute callback and remove the listener
                if (!evt.Params.Contains(cmd)) return;
                responseCallback((ISFSObject) evt.Params["params"]);
                sfsx.RemoveEventListener(SFSEvent.EXTENSION_RESPONSE, listener);
            };
           
            // Add delegate
            sfsx.AddEventListener(SFSEvent.EXTENSION_RESPONSE, listener);
            sfsx.Send(request);
        }
       
        public static async Task<ISFSObject> Send(this SmartFox sfsx, string cmd, ExtensionRequest request) {

            return await Task.Run(() => {
               
                var callbackReceived = new TaskCompletionSource<ISFSObject>();
                Send(sfsx, cmd, request, o => callbackReceived.TrySetResult(o));

                return callbackReceived.Task;
            });
        }
    }


This is mostly pseudocode, havent tested it yet... but it would probably work. It basically just adds a event listener till it received an response and then removes it again... doing this we could simulate such a web api like workflow.

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 57 guests