Problems IP-messagin and Twilio
Problems IP-messagin and Twilio
我使用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条信息是"最近的",另一条是"旧的"。
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()时。
有什么教程可以构建这样的完整聊天吗?或者,你对修改什么以及如何检查问题的有什么建议吗
感谢您的
执行getChannels()
时,您将接收客户端已知的所有通道。尽管您无权从未加入的频道获取成员和消息。因此,您可能需要将代码更改为:
_messagingClient.getChannels()
.then(channels => channels
.filter(channel => channel.status === 'joined'))
.forEach(channel => {
channel.getMembers();
channel.getMessages();
etc...
})
);
相关文章:
- Selenium WebDriver and JavaScript change
- Twilio-显示所有连接参与者的远程参与者视频
- PHP and Javascript functions
- Javascript Return and if/else
- SetTimeout and clearTimeout in Javascript
- lightbox in html 5 and javascript
- TimelineJS and AngularJS
- timeago.js with datatable and PHP
- javascript button ajax and php
- Ajax and Json with Rails
- Combine onload, onresize and onclick
- php布尔值's小写AND大写和数字布尔值'可以接受
- setTimeout and V8
- Jquery post and onclick
- Moment js and IOS
- Rails 3.2 and Paloma gem
- Backbone, TypeScript, and super
- iframe and JavaScript on different pages
- JSON and jQuery Search
- Problems IP-messagin and Twilio