Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Pubnub在更改频道时获取重复消息_Javascript_Php_Jquery_Ajax_Pubnub - Fatal编程技术网

Javascript Pubnub在更改频道时获取重复消息

Javascript Pubnub在更改频道时获取重复消息,javascript,php,jquery,ajax,pubnub,Javascript,Php,Jquery,Ajax,Pubnub,我对pubnub有问题。 我有一个朋友列表,我需要通过切换到另一个朋友来改变pubnub频道,点击他们,与他聊天。 我有一个全局通道变量,我会在“朋友”单击时更改它。问题是,当我切换到另一个朋友并编写消息时,面板中显示的消息是重复的 这是我正在使用的代码 base = "/"; pubnub = ""; channel =""; messageListContent = "ul.chat-messages-block"; function handleMessage(message,$inde

我对pubnub有问题。 我有一个朋友列表,我需要通过切换到另一个朋友来改变pubnub频道,点击他们,与他聊天。 我有一个全局通道变量,我会在“朋友”单击时更改它。问题是,当我切换到另一个朋友并编写消息时,面板中显示的消息是重复的

这是我正在使用的代码

base = "/";
pubnub = "";
channel ="";
messageListContent = "ul.chat-messages-block";

function handleMessage(message,$index) {

    if ( $index != 'me' ) {
        var $index = 'left';        
    } else {
        var $index = 'right';                   
    }   

    var $imageUrl = "";
    if ( message.picture != '' && message.picture != null ) {
        $imageUrl = message.picture;

        if ( (/^http:\/\//.test( $imageUrl ) ) ) {
            $imageUrl = $imageUrl;
        } else {
            $imageUrl = "uploads/user/"+ $imageUrl;
        }
    } else {
        $imageUrl = 'resources/images/user-male.png';
    }               

    var messageEl = jQuery('<li class="'+$index+' clearfix">'+
        '<div class="user-img pull-'+$index+'"> <img src="' + $imageUrl +'" alt="'+message.username+'"> </div>'+
        '<div class="chat-body clearfix">'+
            '<div class="">'+
                '<span class="name">'+message.username+'</span><span class="name"></span><span class="badge"><i class="fa fa-clock-o"></i>'+message.chat_date+'</span></div>'+
                '<p>'+ message.message + '</p>'+
            ' </div>'+
        '</li>');

    jQuery(messageListContent).append(messageEl);
};


jQuery.getJSON( "/chat/read", function( data ) {
    var items = [];
    if ( data != null && data != "" ){

        pubnub = PUBNUB.init({
            publish_key: data.publish_key,
            subscribe_key: data.subscribe_key,
        });

        if ( data.messages.length > 0 ) {

            var $my_id = data.current_user; 
            for( var i = 0; i < data.messages.length; i++ ) {

                if ( data.messages[i].user_id == $my_id ) {
                    $index = "me";                      
                } else {
                    var $index = "";                        
                }
                handleMessage(data.messages[i],$index);
            }
        }

    }
});

jQuery(document).ready(function () {    
    jQuery('#sendMessageButton').click(function (event) {
        var message = jQuery('#messageContent').val();
        var friend_id = jQuery('li.activeChannel').attr('data-id');

        if ( message != '' ) {

            jQuery.ajax({
                url:  base+"chat/sendChat",
                type:'POST',
                data:{
                    friend_id: friend_id,
                    text:message
                },
                success:function(data){
                    var data = JSON.parse(data);
                    //sounds.play( 'chat' );

                    pubnub.publish({
                        channel: channel,
                        message: {
                            username: data.messages.username,
                            message: data.messages.message,
                            user_id: data.messages.friend_id,
                            current_user: data.messages.user_id,
                            picture: data.messages.picture,
                            type:'message',
                            chat_date: data.messages.chat_date
                        }
                    });

                },
                error: function(err){
                    jQuery('.errorText').fadeIn();
                }
            });         
            jQuery('#messageContent').val("");

        }
    });



// Also send a message when the user hits the enter button in the text area.
    jQuery('#messageContent').bind('keydown', function (event) {
        if((event.keyCode || event.charCode) !== 13) return true;
        jQuery('#sendMessageButton').click();
        return false;
    });




jQuery('ul.chat-users li').click(function(){
        var friend_id = jQuery(this).attr('data-id');
        jQuery('ul.chat-users li').removeClass('activeChannel');
        jQuery(this).addClass('activeChannel');

        jQuery.ajax({
            url:  base+"chat/getUsersChat",
            type:'POST',
            data:{
                friend_id: friend_id
            },
            success:function(data){
                var data = JSON.parse(data);
                jQuery('.chat-messages ul').html("");
                //id = pubnub.uuid();
                //channel = 'oo-chat-' + id+friend_id;
                channel = 'oo-chat-' + data.channel;

                if ( data.messages.length > 0 ) {

                    var $my_id = data.current_user; 
                    for( var i = 0; i < data.messages.length; i++ ) {

                        if ( data.messages[i].user_id == $my_id ) {
                            $index = "me";                      
                        } else {
                            var $index = "";                        
                        }
                        //messageListContent = "ul.activeChannel"+channel;
                        //console.log(channel);
                        handleMessage(data.messages[i],$index);
                    }
                }

                pubnub.subscribe({
                    channel: channel,
                    message: function(message) {
                        console.log("Pubnub callback", message);
                        handleMessage(message,"me");
                    },   
                    connect: function(message) {
                        console.log("Pubnub is connected", message);
                    },
                    //callback: 
                });

            },
            error: function(err){
                jQuery('.errorText').fadeIn();
            }
        });
    });

});
base=“/”;
pubnub=“”;
频道=”;
messageListContent=“ul.chat messages block”;
函数handleMessage(消息,$index){
如果($index!=“me”){
var$指数='左';
}否则{
var$指数='右';
}   
var$imageUrl=“”;
if(message.picture!=''&&message.picture!=null){
$imageUrl=message.picture;
if((/^http:\/\/.test($imageUrl))){
$imageUrl=$imageUrl;
}否则{
$imageUrl=“uploads/user/”+$imageUrl;
}
}否则{
$imageUrl='resources/images/user male.png';
}               
var messageEl=jQuery('
  • '+ ' '+ ''+ ''+ ''+消息.用户名+''+消息.聊天时间+''+ “”+message.message+”

    ”+ ' '+ “
  • ”); jQuery(messageListContent).append(messageEl); }; jQuery.getJSON(“/chat/read”),函数(数据){ var项目=[]; 如果(数据!=null&&data!=“”){ pubnub=pubnub.init({ publish_key:data.publish_key, 订阅密钥:data.subscribe\u key, }); 如果(data.messages.length>0){ var$my\u id=data.current\u用户; 对于(var i=0;i0){ var$my\u id=data.current\u用户; 对于(var i=0;i
    下面是它的样子

    有什么想法吗? 我甚至尝试在朋友点击时取消订阅上一个频道,但没有结果。
    我做错了什么?

    我解决了这个问题。问题出在
    publinub.js
    版本中,它是
    3.4
    ,我切换到
    3.7.1
    ,并添加了
    jQuery('ul.chat-users li').click(function(){
            var friend_id = jQuery(this).attr('data-id');
            jQuery('ul.chat-users li').removeClass('activeChannel');
            jQuery(this).addClass('activeChannel');
    
            if ( channel != "" ) {
                pubnub.unsubscribe({
                    channel : channel,
                });
            }