FDC3(Financial Desktop Connectivity and Collaboration Consortium)是一个用于金融桌面应用程序之间互操作性的开放标准。它允许不同的应用程序在同一桌面上进行通信和协作。FDC3 提供了多种 API,包括 intent
、context
和 channel
,以便应用程序可以发送和接收消息。
在多个实例中应用 FDC3 时,通常需要处理以下场景:
- 多个应用程序实例之间的通信:不同的应用程序实例可能需要共享上下文或触发意图。
- 同一应用程序的多个实例之间的通信:同一个应用程序的多个实例可能需要共享数据或同步状态。
示例代码
以下是一个简单的示例,展示了如何在多个应用程序实例之间使用 FDC3 进行通信。
1. 发送 Intent 和 Context
假设你有两个应用程序实例,App A
和 App B
。App A
发送一个 ViewChart
意图,并附带一个包含股票代码的上下文。
javascript
// App A
const fdc3 = await fin.FDC3.getFDC3();
const context = {
type: 'fdc3.instrument',
name: 'AAPL',
id: {
ticker: 'AAPL'
}
};
// 发送 ViewChart 意图
fdc3.raiseIntent('ViewChart', context).then(intentHandler => {
console.log('Intent raised successfully');
}).catch(err => {
console.error('Failed to raise intent:', err);
});
2. 接收 Intent 和 Context
在 App B
中,你需要监听 ViewChart
意图,并处理传入的上下文。
javascript
// App B
const fdc3 = await fin.FDC3.getFDC3();
// 监听 ViewChart 意图
fdc3.addIntentListener('ViewChart', context => {
console.log('Received ViewChart intent with context:', context);
// 根据上下文显示图表
if (context.type === 'fdc3.instrument') {
const ticker = context.id.ticker;
displayChart(ticker);
}
});
function displayChart(ticker) {
// 根据股票代码显示图表
console.log(`Displaying chart for ${ticker}`);
}
3. 使用 Channels 进行通信
FDC3 还支持通过 channels
进行通信。你可以创建一个频道,多个应用程序实例可以加入该频道并广播消息。
javascript
// App A
const fdc3 = await fin.FDC3.getFDC3();
const channel = await fdc3.getOrCreateChannel('myChannel');
// 加入频道
channel.join().then(() => {
console.log('Joined channel successfully');
}).catch(err => {
console.error('Failed to join channel:', err);
});
// 广播消息
const context = {
type: 'fdc3.instrument',
name: 'AAPL',
id: {
ticker: 'AAPL'
}
};
channel.broadcast(context);
javascript
// App B
const fdc3 = await fin.FDC3.getFDC3();
const channel = await fdc3.getOrCreateChannel('myChannel');
// 加入频道
channel.join().then(() => {
console.log('Joined channel successfully');
}).catch(err => {
console.error('Failed to join channel:', err);
});
// 监听频道消息
channel.addContextListener(context => {
console.log('Received context from channel:', context);
if (context.type === 'fdc3.instrument') {
const ticker = context.id.ticker;
displayChart(ticker);
}
});
function displayChart(ticker) {
// 根据股票代码显示图表
console.log(`Displaying chart for ${ticker}`);
}
总结
- Intents :用于触发特定的操作,如
ViewChart
。 - Context:用于传递数据,如股票代码。
- Channels:用于多个应用程序实例之间的广播通信。
通过这些 API,你可以在多个 FDC3 应用程序实例之间实现复杂的通信和协作。