The Remote Procedure Call module for communicating with other WebViews. You can
perform these operations as part of a WebView object.
Members
-
enable
-
Indicates whether to allow RPC events on the WebView. A value oftrueindicates to allow RPC events to be used in the WebView,falseindicates otherwise.Example
webview.rpc.enable = true;
Methods
-
emit(eventName, args, options)
-
Emits a RPC event for the WebView.Parameters:
Name Type Description eventNameString The name of the event to emit. argsObject An array of arguments to pass to the event listeners. optionsObject The options to pass. Example
webview.rpc.emit("message", ["Hello", "World"], {sync:true}); -
on(eventName, callback)
-
Listens for RPC events on the specified WebViewParameters:
Name Type Description eventNameString The event name to start listening for. callbackcallback The callback function to call when the eventNameis received.Example
webview.rpc.on("message", function (options, message) ) { if (options.webviewId === 3) { console.log(message); } }); -
once(eventName, callback)
-
Listens for a certain RPC event on the WebView only once.Parameters:
Name Type Description eventNameString The event name to start listening for. callbackcallback The callback function to call when the eventNameis received.Example
webview.rpc.once("message", function (options, message) { if (options.webviewId === 3) { console.log(message); } }); -
postMessage(messageType, message, optionsopt) → {Object}
-
Sends a message to the JavaScript context of the specified WebView.Parameters:
Name Type Attributes Description messageTypeString The type of message to send. messageObject The message to send as a string or JSON-formatted object. optionsObject <optional>
The RPC options. Returns:
Error An object containing error information if an error occurrs.- Type
- Object
Example
webview.rpc.postMessage("message", "Hello"); webview.rpc.postMessage("error", "Error message"); -
un(eventName, callback)
-
Indicates to stop listening for certain RPC events on the WebView.Parameters:
Name Type Description eventNameString The event name to stop listening for. callbackcallback The callback function to stop calling when the eventNameis received.Example
// Start listening for the event called message webview.rpc.on("message", myCallbackFunction); // Stop listening for the event called message webview.rpc.un("message", myCallbackFunction);