peer.js聊天应用程序中未收到该消息

时间:2019-01-04 13:47:28

标签: javascript html peer peerjs

我正在使用peer.js创建聊天应用程序,但是我无法接收其他实体发送的消息。请有人帮助,也请解释答案:

$('#proceed').click(function()
        {
            if($('#joinBtn').prop("checked"))
            {
                if($('#identity').val()==="")
                {
                    $('#noteStyle').attr("class","alert alert-warning");
                    $('#error').text("Enter friend's ID to connect to.");
                    $('#errorMsg').fadeOut(300);
                }
                else
                {
                    peer = new Peer({key:'n0ei2j1souk57b9'});
                    console.log('peer created');
                    var fID = $("#identity").val();
                    conn = peer.connect(fID);
                    console.log('peer trying to connect');
                    conn.on('open', function()
                    {
                        console.log('connection opened');
                        $('#noteStyle').attr("class","alert alert-success");
                        $('#error').text("Successfully connected.");
                        $('#errorMsg').fadeOut(300);
                        $('#msg').removeAttr("disabled",false);
                        $('#sendBtn').removeAttr("disabled",false);
                        conn.on('data', function(msg)
                        {
                            console.log(data);
                            $('#area').append("<div class='alert bg-yellow text-left fade in show alert-dismissible'><a href='#' class='close' data-dismiss='alert' aria-label='close'>"+"&times;"+"</a><strong>"+msg+"</strong></div>"); 
                            $("html, body").animate({ scrollTop: $(document).height() }, 10);
                        });
                        $('#sendBtn').click(function()
                        {
                            if($('#msg').val()!=="")
                            {
                                $('#area').append("<div class='alert bg-green text-right fade in show alert-dismissible'><a href='#' class='close' data-dismiss='alert' aria-label='close'>"+"&times;"+"</a><strong>"+$('#msg').val()+"</strong></div>"); 
                                $('#msg').val("");
                                $("html, body").animate({ scrollTop: $(document).height() }, 10);
                                document.getElementById("notify").play();
                                conn.send($('#msg').val());
                                console.log('msg sent');
                            }
                        });

                        $('#msg').bind("keypress",function(e)
                        {
                            if(e.which===13 && $('#msg').val()!=="")
                            {
                                $('#area').append("<div class='alert bg-green text-right fade in show alert-dismissible'><a href='#' class='close' data-dismiss='alert' aria-label='close'>"+"&times;"+"</a><strong>"+$('#msg').val()+"</strong></div>"); 
                                $('#msg').val("");
                                $("html, body").animate({ scrollTop: $(document).height() }, 10);
                                document.getElementById("notify").play();
                                conn.send($('#msg').val());
                                console.log('msg sent');
                            }
                        });
                    });
                }
            }
            if($('#hostBtn').prop("checked"))
            {
                peer = new Peer({key:'n0ei2j1souk57b9'});
                console.log('host peer created');
                peer.on('open', function(id)
                {
                    console.log('host peer opened');
                    $('#identity').val(id);
                    $('#noteStyle').attr("class","alert alert-success");
                    $('#error').text("Started Successfully!");
                    $('#errorMsg').show().fadeOut(3000);
                });

                peer.on('connection', function()
                {
                    console.log('host peer got connected');
                    $('#noteStyle').attr("class","alert alert-success");
                    $('#error').text("New user connected.");
                    $('#errorMsg').show().fadeOut(3000);
                    $('#msg').removeAttr("disabled",false);
                    $('#sendBtn').removeAttr("disabled",false);

                    peer.on('data', function(data)
                    {
                       console.log('host peer recieved data');
                       console.log(data);
                    });
                });
            }
        });

我要做的是: --->用户可以选择“主机”或“加入”选项,当选定主机时,其他用户可以连接并且通信继续。 ->选择加入后,用户可以使用ID连接到另一个。 ->连接正常进行,但是消息发送和接收不起作用。

0 个答案:

没有答案