我正在使用Twilio进行IP消息传递。我想在我的移动混合应用程序中创建一个聊天,我正在尝试获得whatsup样式,具有已发送和已读消息的状态。 在最初兴奋的Twilio库之后,现在没有任何工作:(。聊天工作的次数是40%,其他时候我得到了几个错误。在查看错误之前,我想解释一下我是如何构建代码的。
我们的想法是,一旦记录,就会询问该用户的所有频道并将其存储在self.channels
中,以便我可以直观显示该用户的最后一次会话。
1)第一步是在用户登录我的应用程序后立即询问令牌。
setChatToken = function()
{
_token = data.token;
var accessManager = new Twilio.AccessManager(_token);
_messagingClient = new Twilio.IPMessaging.Client(accessManager);
_messagingClient.on('channelUpdated', function(channel)
{
for ( c in self.channels)
{
if (self.channels[c].sid == channel.sid)
{ self.channels[c] = channels; }
}
});
// return a promise;
}
2)然后我问所有频道
// init chat system
setChatToken()
.then(function()
{
// Listen for new invitations to your Client
_messagingClient.on('channelInvited', function(channel)
{
// Automatically join channels that you were invited to
channel.join().then(function(channel)
{
handleMessageEvents(channel);
channel.attributes['him'] = channel['createdBy'];
// I'll separate old, recent and new msgs
channel['recent-messages'] = [];
channel['new-messages'] = [];
self.last_channels.push(channel);
});
});
// asks all the channels
getChannels()
.then(function()
{ // chat init ready
self.ready = true;
});
}
)
.catch(function(err){console.error(err);});
3)这是询问所有频道的代码。我检查是否有新消息正在查看channel.lastConsumedMessageIndex
。我将最近7条消息视为“最近的消息”,将另一条消息视为' old'。
var getChannels = function()
{
// Get Messages for previously created channels
_messagingClient.getChannels()
.then(function(channels)
{ // GET CHANNELS
for (i in channels)
{
var channel = channels[i];
channel['recent-messages'] = [];
channel['old-messages'] = [];
channel['new-messages'] = [];
handleMessageEvents(channel);
self.last_channels.push(channel);
}
})
.then(function()
{ // GET MEMBERS
var promises_update_read_index = [];
for (i in self.last_channels)
{
var channel = self.last_channels[i];
var promise =
channel.getMembers()
.then(function(channelMembers)
{
// check the last message read from the other user, to know
// if there are new messages not read
for(m in channelMembers)
{
var member = channelMembers[m];
if (member.identity != me.id)
{
channel.attributes['him'] = member.identity;
channel.attributes['hisLastMsgIndex'] = member.lastConsumedMessageIndex;
// last message consumed update
member.on('updated', function(updatedMember)
{
//handle the read status information for this member
updateHisMessageReadStatus(updatedMember.identity,
updatedMember.lastConsumedMessageIndex,
updatedMember.channel.sid);
});
}
else
{ // it's me
}
}
}
);
promises_update_read_index.push(promise);
}
return $q.all(promises_update_read_index);
})
.then(function()
{ //GET MESSAGES
var promises_update_messages = [];
for (i in self.last_channels)
{
var channel = self.last_channels[i];
//determine the newest message index
var areThereNewMsgs = false;
var lastConsumedMessageIndex = channel.lastConsumedMessageIndex;
// consider the last 7 messages as 'recent', the other as 'old'
var promise =
channel.getMessages(7)
.then(function(lastChannelMessages)
{
for (j in lastChannelMessages)
{
var message = lastChannelMessages[j];
if (
message.author != me.id &&
message.index > lastConsumedMessageIndex
)
{ // there are new messages
var hisId = channel.attributes['him'];
channel['new-messages'].push(message);
}
else
{
if ( channel.attributes['hisLastMsgIndex']
< channel.lastConsumedMessageIndex
)
{ message['msg-read'] = false;}
else
{ message['msg-read'] = true; }
channel['old-messages'].push(message);
}
if (message.author != me.id)
{ message['msg-read'] = false; } //hide for him
}
});
promises_update_messages.push(promise);
}
return $q.all(promises_update_messages);
})
.catch(function(err){console.error('getMessages '+JSON.stringify(err));})
.then(function()
{
$rootScope.$broadcast(self.CHANNEL_UPDATE_EVENT);
deferred.resolve(true); // i'm ready
})
.catch(function(err)
{
console.error(err);
deferred.reject(err);
});
return deferred.promise;
}
一个问题是,当我创建频道时,其他用户通常不会自动加入频道,因此我会发送从未发送的消息。 另一个问题是
getMembers {"status":403,"description":"Forbidden","body":{"message":"Access forbidden","status":403}}
当我按照之前的定义调用getChannels()时。
是否有任何教程可以构建这样的完整聊天?或者,您是否有关于修改内容以及如何检查问题的建议&#39;
谢谢你
答案 0 :(得分:0)
执行getChannels()
时,您将接收客户已知的所有频道。
虽然您没有权限从您未加入的频道获取成员和消息。
因此,您可能希望将代码更改为:
_messagingClient.getChannels()
.then(channels => channels
.filter(channel => channel.status === 'joined'))
.forEach(channel => {
channel.getMembers();
channel.getMessages();
etc...
})
);