01 大纲
原文链接:
https://help.genesys.com/developer/cic/docs/icws/webhelp/ConceptualContent/GettingStarted_Messaging.htm#top
订阅和消息传递API ICWS
为交互中心系统发生更改时通知应用程序提供了一种方式。有关订阅概念的更多信息,请参阅“ 设计原则”页面。
本章包含以下部分:
- 管理订阅
- 检索邮件
一、管理订阅
订阅是客户端应用程序如何表达对接收有关交互中心系统中某些更改的消息的兴趣。/icws/{sessionId}/messaging/subscriptions/...
提供了以路径开头的资源来操作每个订阅。使用该PUT
方法创建和修改订阅,并使用该方法取消订阅DELETE
。每个订阅都有一个关联的消息类型,客户端应用程序可以在该订阅处于活动状
JavaScript示例
示例订阅用于交互中心系统中各种用户的状态。
要创建此订阅,客户端应用程序必须使用PUT / icws / {sessionId} / messaging / subscriptions / status / user-status方法,并提供userIds
应用程序有兴趣接收状态信息的列表。
下面显示的客户端应用程序可能会如何创建一个用户状态订阅两个用户,其一个例子userIds
是user1
和user2
。
// This example snippet assumes the existence of a helper
// method for sending a request to the active ICWS session.
var sendRequest = function(httpMethod, path, requestPayload, responseCallback) { };
// The type of the message associated with the user-statuses subscription
userStatusMessageType = 'urn:inin.com:status:userStatusMessage'
/**
* Starts the subscription for users statuses for user1 and user2.
* @see stopUserStatusSubscription
*/
function startUserStatusSubscription() {
// The PUT request requires a list of userIds
payload = { userIds:['user1', 'user2'] };
session.sendRequest('PUT', '/messaging/subscriptions/status/user-statuses', payload,
function(status, jsonResponse) {
if ((status >= 200) && (status <= 299)) {
// Subscription was started
}
else {
// There was an error starting the subscription.
// Details are in the jsonResponse.
throw new Error(jsonResponse)
}
});
}
/**
* Stops the subscription for users statuses.
* @see startUserStatusSubscription
*/
function stopUserStatusSubscription() {
// The DELETE request does not take any payload values.
payload = {};
session.sendRequest('DELETE', '/messaging/subscriptions/status/user-statuses', payload,
function(status, jsonResponse) {
if ((status >= 200) && (status <= 299)) {
// Subscription was stopped
}
else {
// There was an error stopping the subscription.
// Details are in the jsonResponse.
throw new Error(jsonResponse)
}
});
}
function displayUserStatuses(userStatusMessage) {
var statusIndex, statusCount, currentStatus;
if (!userStatusMessage.isDelta) {
// If the message is not a delta, we must clear all previous
// information and prepare for refreshed information
clearStatusDisplay();
}
// process the userStatusMessage to update the display
statusCount = userStatusMessage.userStatusList.length;
for (statusIndex = 0; statusIndex < statusCount; statusIndex++) {
currentStatus = userStatusMessage.userStatusList[statusIndex];
updateStatusForUser(currentStatus.userId, currentStatus.statusId);
}
}
// Register a callback with the message channel (see example in the Retrieving Messages section)
// Note: The registration happens before the subscription is started to ensure no messages are missed.
registerMessageCallback(userStatusMessageType, displayUserStatuses);
// Start the subscription
startUserStatusSubscription();
// The subscription can be stopped using the following:
stopUserStatusSubscription();
二、检索邮件
在客户端应用程序中有两种可能的接收事件消息的方法:
使用第一种方法 - 短轮询 - 事件消息在ICWS
服务器上的消息传递通道中排队,直到客户端应用程序请求消息从队列中删除并发送到应用程序的内容。
通过GET / icws / {sessionId} / messaging / messages方法检索消息。客户端应用程序应定期轮询此资源以获取消息。
使用第二种方法 - 服务器发送的事件 - 事件消息在ICWS
服务器上的消息传递通道中可用时发送。在这种情况下,没有必要定期轮询GET / icws / {sessionId} / messaging / messages资源; 但是,必须实例化适当的事件源对象。在使用服务器发送的事件之前,客户端应用程序应确保ICWS
服务器上支持此方法。这可以通过检查该功能的GET / icws / connection / features资源来完成messaging
。messaging
版本2及更高版本支持服务器发送的事件。或者,客户端应用程序可以从features
201 HTTP响应的属性中检索支持的功能版本POST / icws / connection如果include
提供包含该features
值的查询参数。有关处理版本控制的更多信息,请参阅版本控制页面。
注意事项中的参考链接:
https://dev.windows.com/microsoft-edge/platform/status/serversenteventseventsource
JavaScript示例
以下提供了从JavaScript检索挂起消息的简单示例。
// This example snippet assumes the existence of a helper
// method for sending a request to the active ICWS session.
var sendRequest = function(httpMethod, path, requestPayload, responseCallback) { };
// This example snippet assumes the existence of a helper function:
// A function determining whether server-sent events messaging is supported by inspecting the messaging feature version (supported in messaging version 2).
var isServerSentEventsSupportedOnServer = function() {};
// Dictionary of ICWS message __type ID to the callback (type: icwsMessageCallback)
// to invoke when that message is received.
var icwsMessageCallbacks = {};
var messageProcessingTimerId;
var eventSourceInstance;
// Polling interval for retrieving ICWS message queue.
var ICWS_MESSAGE_RETRIEVAL_INTERVAL_MS = 1000;
/**
* Sets the callback for a particular type of ICWS message.
* @param {String} messageType The ICWS message type. (ex: urn:inin.com:status:userStatusMessage)
* @param {icwsMessageCallback} messageCallback The callback to invoke with the message details.
* @throws {Error} The messageCallback was undefined.
* @throws {Error} A callback is already registered for the specified messageType.
*/
function registerMessageCallback(messageType, messageCallback) {
if (messageCallback === undefined) {
throw new Error('Invalid argument "messageCallback".');
}
if (!icwsMessageCallbacks[messageType]) {
icwsMessageCallbacks[messageType] = messageCallback;
} else {
throw new Error('Message callback already registered for message type: ' + messageType);
}
};
/**
* Starts the message processing mechanism, if not already running.
* @see stopMessageProcessing
*/
function startMessageProcessing() {
if (isServerSentEventsSupportedOnServer() && typeof EventSource !== 'undefined') {
if (!eventSourceInstance || eventSourceInstance.readyState === EventSource.CLOSED) {
var messagingUrl = getSessionUrl('/messaging/messages');
eventSourceInstance = new EventSource(messagingUrl, {
withCredentials: true // Allows the Cookie HTTP request header to be sent
});
eventSourceInstance.onmessage = onServerSentEventMessage;
}
} else {
// Only send the next request once the previous result has been received.
function runTimerInstance() {
messageProcessingTimerCallback();
messageProcessingTimerId = setTimeout(runTimerInstance, ICWS_MESSAGE_RETRIEVAL_INTERVAL_MS);
}
if (!messageProcessingTimerId) {
runTimerInstance();
}
}
}
/**
* Stops the message processing mechanism, if running.
* @see startMessageProcessing
*/
function stopMessageProcessing() {
if (eventSourceInstance) {
if (eventSourceInstance.readyState !== EventSource.CLOSED) {
eventSourceInstance.stop();
}
} else if (!!messageProcessingTimerId) {
clearTimeout(messageProcessingTimerId);
messageProcessingTimerId = null;
}
}
/**
* Implements the message processing mechanism timer callback.
* @see startMessageProcessing
* @see stopMessageProcessing
*/
function messageProcessingTimerCallback() {
var payload, messageIndex, messageCount, jsonMessage, messageType, messageCallback;
// The messaging GET request does not take any payload values.
payload = {};
sendRequest('GET', '/messaging/messages', payload, function(status, jsonResponse) {
if ((status >= 200) && (status <= 299)) {
// Process retrieved messages.
messageCount = jsonResponse.length;
for (messageIndex = 0; messageIndex < messageCount; messageIndex++) {
jsonMessage = jsonResponse[messageIndex];
messageType = jsonMessage.__type;
// For each message, invoke a registered message callback if there is one.
messageCallback = icwsMessageCallbacks[messageType];
if (messageCallback) {
messageCallback(jsonMessage);
} else {
// No registered message handler.
}
}
}
});
/**
* Handles eventSourceInstance's message event and relays it to the correct callback.
* @param {Event} event The onmessage event data.
* @see stopMessageProcessing
*/
function onServerSentEventMessage(event) {
var message, messageType, messageCallback;
try {
// Process the data off of the event. It is a JSON string.
var message = JSON.parse(event.data);
messageType = message.__type;
// Invoke a registered message callback if there is one.
messageCallback = icwsMessageCallbacks[messageType];
if (messageCallback) {
messageCallback(jsonMessage);
} else {
// No registered message handler.
}
}
catch (error) {
// Failed to process message.
}
}
}