Skip to content

Working with events

Eva edited this page Jul 15, 2021 · 4 revisions

With this CEF plugin, you can communicate between your game mode and clients' web pages. Events can make your page alive!

Pawn API:

  • cef_emit_event(player_id, const event_name[], args…)
  • cef_subscribe(const event_name[], const callback[])

Browser (JS) API:

  • cef.on(event_name, callback)
  • cef.emit(event_name, args…)

We will create a login page for example.

Start with a game mode:

#include <cef>

#define LOGIN_BROWSER_ID 0x12345
#define LOGIN_BROWSER_URL "http://your-hosting.com"

forward OnLogin(player_id, const arguments[]);

public OnCefInitialize(player_id, success) {
    if (success == 1) {
        cef_create_browser(player_id, LOGIN_BROWSER_ID, LOGIN_BROWSER_URL, false, true);
        return;
    }

    SendClientMessage(player_id, -1, "Ahh to bad you cannot see our new login page ...");
    // show a dialog as you would do without a plugin
}

public OnCefBrowserCreated(player_id, browser_id, status_code) {
    if (browser_id == LOGIN_BROWSER_ID) {
        if (status_code != 200) {
            // fallback to dialogs ...
            return;
        }

        // subscribe on a login event
        cef_subscribe("loginpage:login", "OnLogin");
    }
}

public OnLogin(player_id, const arguments[]) {
    // get a user password and compare it with a passed string
    new success = compare_player_password(player_id, arguments);

    // send a response status to the player
    // CEFINT(value) for integers
    // CEFFLOAT(value) for floats
    // CEFSTR(value) for string ...
    // cef_emit_event(player_id, "someevent", CEFINT(0), CEFFLOAT(25.51), CEFSTR("hellow!"));
    cef_emit_event(player_id, "loginpage:response", CEFINT(success));

    if (success) {
        // your code when user is logged in
        OnSuccessLogin(player_id);
    }
}

The next step is to create a login page. I will skip the HTML part (you can find an example here)

<!DOCTYPE html>
<html>
<body>
    <div>
        <!-- your login page here ... -->
    </div>
    <script>
        const sendCredButton = 0; // document.getElementById for example

        // subscribe to the login response
        cef.on("loginpage:response", (success) => {
            if (success) {
                // show something or do somethig on the page
                showSuccessStatusAndSomethingElse();

                // hide a web page in 5 seconds
                setTimeout(() => cef.hide(true), 5000);

                return;
            }

            // make your function to show an error
            showErrorOnPage("Wrong password, try again.");
        });

        sendCredButton.addEventHandler("click", () => {
            const input = "somepassword"; // get the real string from the input box

            // send a password to the server
            cef.emit("loginpage:login", input);
        });
    </script>
</body>

</html>
Clone this wiki locally