Я создаю приложение чата с использованием 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'>"+"×"+"</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'>"+"×"+"</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'>"+"×"+"</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);
});
});
}
});
Все, что я хочу сделать, это:
---> Пользователь может выбрать опцию Host или Join, когда выбранный хост, другие пользователи могут подключиться и связь продолжается.
-> Когда выбрано соединение, пользователь может подключиться к другому с идентификатором.
-> Соединение установлено отлично, но отправка и получение сообщений не работают.