Mini Posted February 15, 2018 Share Posted February 15, 2018 (edited) I'm struggling understanding how to communicate client data with the browser. I'm not even sure I'm able to communicate my misunderstanding the right way and its driving me nuts lol... Basically I want to check if the player's vehicle is damaged, and if it is, show a "Fix" button in the browser. In /browser/js/main.js I am using a boolean variable "vehicleIsDamaged" in an if statement to handle showing the "Fix" button. Assuming that this is an alright way of going about it - I was going to do something like: browser.execute(`vehicleIsDamaged = true;`); based on checking .isDamaged() with .getVehicleIsIn() but I don't understand where the most appropriate places to call these functions are. Any direction is appreciated. Edit: I think this is exactly what I was looking for. 3 hours ago, ShrewdSpirit said: Hello! Asynchronous Javascript Communication is a module to allow easy communication between server, client and CEF/browser (CEF support will be implemented in a future release). 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") }) Yep! That's it. Very easy! 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 Currently using ajcom in the browser is not implemented and I'll make it happen in the next version BTW ajcom should be included on both client and server scripts (yes, it uses the same script on both sides ) I have more cool modules in mind that will be using ajcom and will share them as soon as they're implemented So keep in touch! Edited February 15, 2018 by Mini Link to comment Share on other sites More sharing options...
ShrewdSpirit Posted February 15, 2018 Share Posted February 15, 2018 (edited) Hello. You can currently use mp.trigger in the browser to call an event in your client scripts and use browser.execute to run a js code in your browser from your client scripts. Using these you can make a bidirectional communication with the client and the browser. And for my module, ajcom, I have CEF communications in todo list and it will be implemented very soon (probably today, after work!). With the CEF coms you can call handlers in any of these directions without having to deal with mp.trigger and browser.execute: cef > client (bidirectional) cef > server (bidirectional) Edited February 15, 2018 by ShrewdSpirit 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now