The goog.messaging.PortChannel Class

goog.messaging.PortChannel
> goog.messaging.AbstractChannel
> goog.Disposable

goog.messaging.PortChannel(underlyingPort)

A wrapper for several types of HTML5 message-passing entities ({@link MessagePort}s and {@link WebWorker}s). This class implements the {@link goog.messaging.MessageChannel} interface. This class can be used in conjunction with other communication on the port. It sets {@link goog.messaging.PortChannel.FLAG} to true on all messages it sends.

underlyingPort {!MessagePort|!WebWorker}
The message-passing entity to wrap. If this is a {@link MessagePort}, it should be started. The remote end should also be wrapped in a PortChannel. This will be disposed along with the PortChannel; this means terminating it if it's a worker or removing it from the DOM if it's an iframe.

.FLAG {string}

The flag added to messages that are sent by a PortChannel, and are meant to be handled by one on the other side.

.addOnDisposeCallback(callback, opt_scope)

Inherited from goog.Disposable .

Invokes a callback function when this object is disposed. Callbacks are invoked in the order in which they were added.

callback {!Function}
The callback function.
opt_scope {Object=}
An optional scope to call the callback in.

.connect()

Inherited from goog.messaging.AbstractChannel .

Immediately calls opt_connectCb if given, and is otherwise a no-op. If subclasses have configuration that needs to happen before the channel is connected, they should override this and {@link #isConnected}.

.creationStack {string}

Inherited from goog.Disposable .

If monitoring the goog.Disposable instances is enabled, stores the creation stack trace of the Disposable instance.

.dispose()

Inherited from goog.Disposable .

Disposes of the object. If the object hasn't already been disposed of, calls {@link #disposeInternal}. Classes that extend {@code goog.Disposable} should override {@link #disposeInternal} in order to delete references to COM objects, DOM nodes, and other disposable objects. Reentrant.

returns {void}
Nothing.

.forEmbeddedWindow(window, peerOrigin, opt_timer)

Create a PortChannel that communicates with a window embedded in the current page (e.g. an iframe contentWindow). The code within the window should call {@link forGlobalWindow} to establish the connection. It's possible to use this channel in conjunction with other messages to the embedded window. However, only one PortChannel should be used for a given window at a time.

window {!Window}
The window object to communicate with.
peerOrigin {string}
The expected origin of the window. See http://dev.w3.org/html5/postmsg/#dom-window-postmessage.
opt_timer {goog.Timer=}
The timer that regulates how often the initial connection message is attempted. This will be automatically disposed once the connection is established, or when the connection is cancelled.
returns {!goog.messaging.DeferredChannel}
The PortChannel. Although this is not actually an instance of the PortChannel class, it will behave like one in that MessagePorts may be sent across it. The DeferredChannel may be cancelled before a connection is established in order to abort the attempt to make a connection.

.forGlobalWindow(peerOrigin)

Create a PortChannel that communicates with the document in which this window is embedded (e.g. within an iframe). The enclosing document should call {@link forEmbeddedWindow} to establish the connection. It's possible to use this channel in conjunction with other messages posted to the global window. However, only one PortChannel should be used for the global window at a time.

peerOrigin {string}
The expected origin of the enclosing document. See http://dev.w3.org/html5/postmsg/#dom-window-postmessage.
returns {!goog.messaging.MessageChannel}
The PortChannel. Although this may not actually be an instance of the PortChannel class, it will behave like one in that MessagePorts may be sent across it.

.isConnected()

Inherited from goog.messaging.AbstractChannel .

Always returns true. If subclasses have configuration that needs to happen before the channel is connected, they should override this and {@link #connect}.

.isDisposed()

Inherited from goog.Disposable .

returns {boolean}
Whether the object has been disposed of.

.registerDefaultService()

Inherited from goog.messaging.AbstractChannel .

.registerDisposable(disposable)

Inherited from goog.Disposable .

Associates a disposable object with this object so that they will be disposed together.

disposable {goog.disposable.IDisposable}
that will be disposed when this object is disposed.

.registerService()

Inherited from goog.messaging.AbstractChannel .

.send(serviceName, payload)

Sends a message over the channel. As an addition to the basic MessageChannel send API, PortChannels can send objects that contain MessagePorts. Note that only plain Objects and Arrays, not their subclasses, can contain MessagePorts. As per {@link http://www.w3.org/TR/html5/comms.html#clone-a-port}, once a port is copied to be sent across a channel, the original port will cease being able to send or receive messages.

serviceName {string}
The name of the service this message should be delivered to.
payload {string|!Object|!MessagePort}
The value of the message. May contain MessagePorts or be a MessagePort.