この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

イベントの再実行サンプル: コードのウォークスルー

CometD 再実行拡張を JavaScript で登録して使用する方法について説明します。

イベントの再実行の JavaScript サンプル

デュラブルストリーミングのサンプルの Visualforce コンポーネントは、再実行オプションで登録する CometD クライアントを実装します。このコンポーネントは Visualforce ページに埋め込まれています。

再実行オプションで CometD クライアントを実装する場合、Visualforce コンポーネントを再利用するか、アプリケーションの JavaScript コードを適合できます。このセクションではサンプルコンポーネント部分が強調表示されています。

まず、Salesforce が提供する CometD 拡張 cometdReplayExtension を登録してイベントを再実行します。このスニペットでは、ストリーミングチャネルと再実行オプションも設定します。registerExtension の最初の引数は、拡張の登録解除に使用する任意の名前です。

1// Register Generic Streaming Replay extension
2var replayExtension = new cometdReplayExtension();
3replayExtension.setChannel(<Streaming Channel to Subscribe to>);
4replayExtension.setReplay(<Event Replay Option>);
5cometd.registerExtension('myReplayExtensionName', replayExtension);

次に、クライアントが CometD 再実行エンドポイントに接続します。エンドポイントの API バージョンは 37.0 以降である必要があります。現在のセッションのセッション ID 値が Authorization ヘッダーで渡されます。クライアントが cometd configure() 関数をコールして接続を設定し、エンドポイントおよび認証ヘッダーを指定します。次に、クライアントが cometd handshake() 関数をコールしてハンドシェイクを実行します。

1// Connect to the CometD endpoint
2cometd.configure({
3    url: window.location.protocol+'//'+window.location.hostname+ 
4        (null != window.location.port ? (':'+window.location.port) : '') +
5        '/cometd/37.0/',
6    requestHeaders: { Authorization: 'OAuth {!$Api.Session_ID}'}
7});
8cometd.handshake();

接続プロセスのすべてのステップが正常に終了したことを確認するため、次のステップに進む前にクライアントはリスナーを使用します。たとえば、/meta/handshake チャネルのリスナーは、ハンドシェイクが成功したかどうかを確認します。成功した場合、subscribe() 関数がコールされます。

1if(!metaHandshakeListener) {
2    metaHandshakeListener = cometd.addListener('/meta/handshake', function(message) {
3        if (message.successful) {
4             $('#content').append('<br><br> DEBUG: Handshake Successful: '+
5                 JSON.stringify(message)+' <br><br>');
6                    
7             if (message.ext && message.ext[REPLAY_FROM_KEY] == true) {
8                 isExtensionEnabled = true;
9             }                    
10             subscribedToChannel = subscribe(channel);
11        } else
12             $('#content').append('DEBUG: Handshake Unsuccessful: '+
13                 JSON.stringify(message)+' <br><br>');
14    });
15}

最後に、CometD subscribe() 関数のコールバックを指定します。チャネルでメッセージが受信されたときに、CometD がこのコールバック関数をコールします。このサンプルでは、コールバック関数によってページにメッセージデータが表示されます。ID 値が "content"div HTML 要素にデータが追加されます。

1function subscribe(channel) {
2    // Subscribe to a topic. 
3        // JSON-encoded update will be returned in the callback.
4    return cometd.subscribe(channel, function(message) {
5        var div = document.getElementById('content');
6        div.innerHTML = div.innerHTML + '<p>Notification ' +
7        'on channel: ' + JSON.stringify(message.channel) + '<br>' +
8        'Payload: ' + JSON.stringify(message.data.payload) + '<br>' + 
9        'Replay Id: ' + JSON.stringify(message.data.event.replayId) + '<br>' + 
10        'Full message: ' + JSON.stringify(message) + '</p><br>';
11    });
12}

cometdReplayExtension 拡張

この拡張は、CometD クライアントの再実行機能の前提条件です。デュラブルストリーミングのサンプルの Visualforce コンポーネントでは、この拡張が登録および使用されています。CometD クライアントを実装する場合、プロジェクトに再実行拡張を含めるだけで、変更する必要はありません。

メモ

cometdReplayExtension には、受信メッセージと送信メッセージでコールされる cometd 拡張関数が含まれます。これらの拡張関数は、ハンドシェイク時に拡張の登録を確認し、登録時に再実行オプションを設定するロジックを実装します。

ハンドシェイク時に、受信メッセージの関数で再実行拡張が登録されているかどうかが確認されます。登録されている場合は、_extensionEnabled 変数が true に設定されます。この関数は受信メッセージの再実行 ID も保存するため、クライアントのタイムアウト後に再接続するときに使用できます。

1this.incoming = function(message) {
2    if (message.channel === '/meta/handshake') {
3        if (message.ext && message.ext[REPLAY_FROM_KEY] == true) {
4            _extensionEnabled = true;
5        } else if (message.channel === _channel && message.data && message.data.event && 
6                   message.data.event.replayId) {
7            _replay = message.data.event.replayId;
8        }
9    }
10}

登録時に、送信メッセージの関数で _extensionEnabled 変数を使用して再実行拡張が登録されているかどうかが確認されます。拡張が登録されている場合、関数は指定された再実行オプションに基づいてイベントに登録します。このサンプルでは、拡張の setReplay() 関数をコールして再実行オプションを設定しています。

1this.outgoing = function(message) {
2    if (message.channel === '/meta/subscribe') {
3        if (_extensionEnabled) {
4            if (!message.ext) { 
5                message.ext = {};
6            }
7            var replayFromMap = {};
8            replayFromMap[_channel] = _replay;
9            // add "ext : { "replay" : { CHANNEL : REPLAY_VALUE }}" 
10            //   to subscribe message.
11            message.ext[REPLAY_FROM_KEY] = replayFromMap;
12        }
13    }
14};