Jump to content

Recommended Posts

Posted (edited)

Hello!

Asynchronous Javascript Communication is a module to allow easy communication between server, client and CEF/browser. This module lets you call server handlers from clients (and vice versa) without dealing with adding and managing custom event handlers.

You can easily call a handler and get your callback called as soon as everything is returned from the handler in a promise like way! Let's see how it works in action:

// server side
const ajcom = require("./ajcom")
ajcom.register("getServerName", hCtx => {
	return mp.config.name
})
// client side
const ajcom = require("./[package name]/ajcom.js")
mp.events.add("guiReady", () => {
	ajcom.callServer("getServerName").then((ctx, serverName) => {
		mp.gui.chat.push(`Welcome to ${serverName} ragemp server!`)
	})
})

That's all! Not convinced yet? See how the above code is done without ajcom:

// server side
mp.events.add("getServerName", (player) => {
	player.call("gotServerName", [mp.config.name])
})
// client side
mp.events.add("gotServerName", (serverName) => {
	mp.gui.chat.push(`Welcome to ${serverName} ragemp server!`)
})
mp.events.add("guiReady", () => {
	mp.events.callRemote("getServerName")
})

See? It eases the event handling mess. But there's a lot more to ajcom. You can easily handle errors happening on handler's side or any of the callbacks, set delays and other stuff. The full documentation is available here and you can grab the module from here

Github repo

 

Changelog

v1.9.6 (2018/3/7)
  • added jsdocs for constructors
  • added separate enums and classes for types used as parameters
  • added noPostback flag
  • added flags for handlers
  • added special handlers (hooks) for server side handlers getting called
  • added the ability to broadcast a call to all or specific clients
  • added cef support
  • added cross side logging
  • fixed calling a handler with triggerDelay not being called
  • fixed data.source and data.target having wrong values
  • change the namespace so that users can use enums and classes
v1.1.3 (2018/2/25)
  • added jsdocs
  • changed event handling codes
  • fixed client > client call
  • fixed some possible crashes (fake client calling server)
v1.0.0 (2018/2/15):
  • Initial release!
Edited by ShrewdSpirit
  • Like 7
  • 3 months later...
Posted (edited)

Hi !

 

I am new in this game, and I have some difficulties to use your module instead of the original way.

 

For exemple, I need to do a registration form and I am stuck when I send a request from client side to know if player's username is already taken, or not.

 

Client-sided code:

function sendCredentials() {
	let name = $('#name').val();
	
	ajcom.callServer('registerPlayer', name).then((ctx, result) => {
        	/* In the future, that part will display error or success message to client */
        	console.log('Client: '+result);
    	});
};

 

Server-sided code:

function chefIfThatPlayerExists(name) {
    return DB.query("SELECT null FROM players WHERE name = ? LIMIT 1", name, function(error, result) {
        if(error) throw error;
        return result.length;
    });
}; // Shittery code, I checked all StackOverflow's problems about that subject and due to asynchrone queries, that will never work....

ajcom.register('registerPlayer', (hCtx, name) => {
    // The question is, how to do some MySQL queries inside ajcom.register eventHandler ?
    const exists = checkIfThatPlayerExists(name);
    console.log(exists);
});

/* In wiki */
// server side
ajcom.register('handler', (hCtx) => {
    if (hCtx.source === ajcom.ScriptType.Client) {
        // query player data
        const playerData = dbGetPlayerData(hCtx.client.name)
        return {money: playerData.money, perm: playerData.perm}
    }
})

Also, I think that I am actually retarded, but I don't understand how I could use your module to simplify the process..

 

I tested all NodeJS standard resolutions about this, but I think that callbacks and promise will not be helpful in this case.. At every solution, your code needs to be inside the MySQL querying function... With the original way, that is pretty easy because I could call a function which returns result to client without any problem, but with Ajcom, problem is real as long as you must return the MySQL result outside it. Holy cow...

 

If you could include an exemple with MySQL queries, that could actually help me a lot... thanks in advance.

 

EDIT : Maybe should I use MySQL2 ( https://www.npmjs.com/package/mysql2#using-promise-wrapper ) ... ?

 

Best regards,

Marcelo

Edited by RealMarcelo
  • 3 weeks later...
  • 1 year later...
Posted

Hey
I have a problem with it.
The Server try to return the Correct Answer, but the Client receive undefined:
 

//SERVER
ajcom.register("phone:getNumber", (hCtx, name) => {
	var entry = {};
  db.numbers.forEach((val) => {
    if(val["username"] == name) {
      entry = val;
    }
  });
  if(entry["username"] == null) {
    return "notexist";
  } else {
    var num = entry["number"];
    console.log(num);
	//ANSWER IN CONSOLE IS 284110
    return num;
  }
});

//CLIENT
ajcom.callServer("phone:getNumber", name).then((ctx, value) => {
	mp.game.graphics.notify(toString(value));
	//MESSAGE IN CLIENT IS [object Undefined]
	telnumber = value;
});

If I return "TEST" instead of num, it return undefined too.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...