messaging.PortChannel Extends goog.messaging.AbstractChannel
A wrapper for several types of HTML5 message-passing entities ( MessagePorts and WebWorkers). This class implements the goog.messaging.MessageChannel interface. This class can be used in conjunction with other communication on the port. It sets goog.messaging.PortChannel.FLAG to true on all messages it sends.

Inheritance

Constructor

goog.messaging.PortChannel(underlyingPort)

Parameters

underlyingPort : !MessagePort | !WebWorker
The message-passing entity to wrap. If this is a 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.

Instance Methods

Public Protected Private
deliver_(e)
Delivers a message to the appropriate service handler. If this message isn't a GearsWorkerChannel message, it's ignored and passed on to other handlers.
Arguments:
e : goog.events.Event
The event.
code »
disposeInternal()
No description.
code »
extractPorts_(portsmessage) string | !Object
Extracts all MessagePort objects from a message to be sent into an array. The message ports are replaced by placeholder objects that will be replaced with the ports again on the other side of the channel.
Arguments:
ports : Array.<MessagePort>
The array that will contain ports extracted from the message. Will be destructively modified. Should be empty initially.
message : string | !Object
The message from which ports will be extracted.
Returns: string | !Object  The message with ports extracted.
code »
injectPorts_(portsmessage) string | !Object
Injects MessagePorts back into a message received from across the channel.
Arguments:
ports : Array.<MessagePort>
The array of ports to be injected into the message.
message : string | !Object
The message into which the ports will be injected.
Returns: string | !Object  The message with ports injected.
code »
send(serviceNamepayload)
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 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.
Arguments:
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.
code »
validateMessage_(data) boolean
Checks whether the message is invalid in some way.
Arguments:
data : Object
The contents of the message.
Returns: boolean  True if the message is valid, false otherwise.
code »
connect()
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 #isConnected.
code »
decodePayload(serviceNamepayloadobjectPayload) string | Object
Converts the message payload into the format expected by the registered service (either JSON or string).
Arguments:
serviceName : string
The name of the service receiving the message.
payload : string | !Object
The contents of the message.
objectPayload : boolean
Whether the service expects an object or a plain string.
Returns: string | Object  The payload in the format expected by the service, or null if something went wrong.
code »
defaultService_()
The default service to be run when no other services match.
code »
deliver(serviceNamepayload)
Delivers a message to the appropriate service. This is meant to be called by subclasses when they receive messages. This method takes into account both explicitly-registered and default services, as well as making sure that JSON payloads are decoded when necessary. If the subclass is capable of passing objects as payloads, those objects can be passed in to this method directly. Otherwise, the (potentially JSON-encoded) strings should be passed in.
Arguments:
serviceName : string
The name of the service receiving the message.
payload : string | !Object
The contents of the message.
code »
disposeInternal()
No description.
code »
getService(serviceNamepayload) ?{callback: function((string | !Object)), objectPayload: boolean
Find the service object for a given service name. If there's no service explicitly registered, but there is a default service, a service object is constructed for it.
Arguments:
serviceName : string
The name of the service receiving the message.
payload : string | !Object
The contents of the message.
Returns: ?{callback: function((string | !Object)), objectPayload: boolean  } The service object for the given service, or null if none was found.
code »
isConnected()
Always returns true. If subclasses have configuration that needs to happen before the channel is connected, they should override this and #connect.
code »
registerDefaultService()
No description.
code »
registerService()
No description.
code »
addOnDisposeCallback(callbackopt_scope)
Invokes a callback function when this object is disposed. Callbacks are invoked in the order in which they were added.
Arguments:
callback : function(this:T):?
The callback function.
opt_scope : T=
An optional scope to call the callback in.
code »
dispose() void
Disposes of the object. If the object hasn't already been disposed of, calls #disposeInternal. Classes that extend goog.Disposable should override #disposeInternal in order to delete references to COM objects, DOM nodes, and other disposable objects. Reentrant.
Returns: void  Nothing.
code »
disposeInternal()
Deletes or nulls out any references to COM objects, DOM nodes, or other disposable objects. Classes that extend goog.Disposable should override this method. Not reentrant. To avoid calling it twice, it must only be called from the subclass' disposeInternal method. Everywhere else the public dispose method must be used. For example:
  mypackage.MyClass = function() {
    mypackage.MyClass.base(this, 'constructor');
    // Constructor logic specific to MyClass.
    ...
  };
  goog.inherits(mypackage.MyClass, goog.Disposable);

  mypackage.MyClass.prototype.disposeInternal = function() {
    // Dispose logic specific to MyClass.
    ...
    // Call superclass's disposeInternal at the end of the subclass's, like
    // in C++, to avoid hard-to-catch issues.
    mypackage.MyClass.base(this, 'disposeInternal');
  };
code »
getDisposed() boolean
Use #isDisposed instead. No description.
Returns: boolean  Whether the object has been disposed of.
code »
isDisposed() boolean
No description.
Returns: boolean  Whether the object has been disposed of.
code »
registerDisposable(disposable)
Associates a disposable object with this object so that they will be disposed together.
Arguments:
disposable : goog.disposable.IDisposable
that will be disposed when this object is disposed.
code »

Instance Properties

constructor :
No description.
Code »
listenerKey_ :
The key for the event listener.
Code »
Logger for this class.
Code »
port_ :
The wrapped message-passing entity.
Code »
constructor :
No description.
Code »
Logger for this class.
Code »
send :
No description.
Code »
services_ :
The services registered for this channel.
Code »
creationStack :
If monitoring the goog.Disposable instances is enabled, stores the creation stack trace of the Disposable instance.
Code »
disposed_ :
Whether the object has been disposed of.
Code »
onDisposeCallbacks_ :
Callbacks to invoke when this object is disposed.
Code »

Static Methods

goog.messaging.PortChannel.forEmbeddedWindow(windowpeerOriginopt_timer) !goog.messaging.DeferredChannel
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 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.
Arguments:
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.
code »
goog.messaging.PortChannel.forGlobalWindow(peerOrigin) !goog.messaging.MessageChannel
Create a PortChannel that communicates with the document in which this window is embedded (e.g. within an iframe). The enclosing document should call 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.
Arguments:
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.
code »

Static Properties

goog.messaging.PortChannel.FLAG :
The flag added to messages that are sent by a PortChannel, and are meant to be handled by one on the other side.
Code »
goog.messaging.PortChannel.REQUIRES_SERIALIZATION_ :
Whether the messages sent across the channel must be JSON-serialized. This is required for older versions of Webkit, which can only send string messages. Although Safari and Chrome have separate implementations of message passing, both of them support passing objects by Webkit 533.
Code »
goog.messaging.PortChannel.superClass_ :
No description.
Code »

Package messaging

Package Reference