public abstract class WebMessagePort extends Object
WebView.createWebMessageChannel()
for creating a message channel.
When a message port is first created or received via transfer, it does not
have a WebMessageCallback to receive web messages. The messages are queued until
a WebMessageCallback is set.
A message port should be closed when it is not used by the embedder application
anymore. A closed port cannot be transferred or cannot be reopened to send
messages. Close can be called multiple times.
When a port is transferred to JS, it cannot be used to send or receive messages
at the Java side anymore. Different from HTML5 Spec, a port cannot be transferred
if one of these has ever happened: i. a message callback was set, ii. a message was
posted on it. A transferred port cannot be closed by the application, since
the ownership is also transferred.
It is possible to transfer both ports of a channel to JS, for example for
communication between subframes.Modifier and Type | Class and Description |
---|---|
static class |
WebMessagePort.WebMessageCallback
The listener for handling MessagePort events.
|
Constructor and Description |
---|
WebMessagePort()
Constructor.
|
Modifier and Type | Method and Description |
---|---|
abstract void |
close()
Close the message port and free any resources associated with it.
|
abstract void |
postMessage(WebMessage message)
Post a WebMessage to the entangled port.
|
abstract void |
setWebMessageCallback(WebMessagePort.WebMessageCallback callback)
Sets a callback to receive message events on the main thread.
|
abstract void |
setWebMessageCallback(WebMessagePort.WebMessageCallback callback,
Handler handler)
Sets a callback to receive message events on the handler that is provided
by the application.
|
public abstract void postMessage(WebMessage message)
message
- the message from Java to JS.IllegalStateException
- If message port is already transferred or closed.public abstract void close()
public abstract void setWebMessageCallback(WebMessagePort.WebMessageCallback callback)
callback
- the message callback.public abstract void setWebMessageCallback(WebMessagePort.WebMessageCallback callback, Handler handler)
callback
- the message callback.handler
- the handler to receive the message messages.