我正在为我的公司创建一个聊天应用程序,每当我尝试连接时,我都会遇到 Web Socket已关闭或已处于关闭状态的问题。但情况是,相同的源代码相同的应用程序在我的朋友笔记本电脑上工作正常,即使它在我的办公室笔记本电脑上正常工作,但在我的家用笔记本电脑中它给我这个错误,我不知道发生了什么。
这是我的ChatSocket.js
代码
var chatsocket = function() {
var messageReceive = "";
var ws;
var messageSend = function(message) {
var buildMessage = "<div class='row msg_container base_sent'>"
+ "<div class='col-md-10 col-xs-10'> "
+ "<div class='messages msg_sent'>" + "<p>" + message + "</p>"
+ "<time datetime='2009-11-13T20:00'>Timothy • 51 min</time>"
+ "</div>" + "</div>" + "";
var imgMessage = "<div class='col-md-2 col-xs-2 avatar'>"
+ "<img src='http://www.bitrebels.com/wp-content/uploads/2011/02/Original-Facebook-Geek-Profile-Avatar-1.jpg'class='img-responsive ' />"
+ "</div>" + "</div>";
return buildMessage + imgMessage;
};
var messageReceive = function(message) {
var imgMessage = "<div class='row msg_container base_receive'>" +
"<div class='col-md-2 col-xs-2 avatar'>"
+ "<img src='http://www.bitrebels.com/wp-content/uploads/2011/02/Original-Facebook-Geek-Profile-Avatar-1.jpg'class='img-responsive ' />"
+ "</div>";
var buildMessage = "<div class='col-md-10 col-xs-10'> "
+ "<div class='messages msg_receive'>" + "<p>" + message + "</p>"
+ "<time datetime='2009-11-13T20:00'>Timothy • 51 min</time>"
+ "</div>" + "</div>" + "</div>";
return imgMessage + buildMessage;
};
var connect = function() {
var user = $("#user").val();
try{
ws = new WebSocket("ws://localhost:8086/EnterpriceChat/chat/" + user);
} catch(err) {
console.log("error");
}
ws.onmessage = function(event) {
var message = JSON.parse(event.data);
var idSuffix = getElementIdSuffix(message.from);
createNewChatBox(message.from);
var msg_panel_id = getMsgPanelId(idSuffix);
$("#"+msg_panel_id).append(messageReceive(message.content));
};
};
var sendMessage = function(element) {
var inputElementId = element.attr("data-id");
var to = element.attr("data-id-email");
var msg_panel_id = element.attr("data-id-msg-panel");
//Validating the inputs
var content = $("#"+inputElementId).val();
//Reset the input fields
$("#"+inputElementId).val('');
if(!content) {
return;
}
var json = JSON.stringify({
"to" : to,
"content" : content
});
ws.send(json);
$("#"+msg_panel_id).append(messageSend(content));
};
var eventClick = function() {
$('.btn-chat-send').unbind('click');
$('.chat_input').unbind('keypress');
$('.btn-chat-send').click(function() {
console.log();
sendMessage($(this));
});
$('.chat_input').keypress(function(e) {
if(e.which == 13) {
$(this).closest('.input-group').find('.btn-chat-send').click();
}
});
};
return {
initEvent : function() {
connect();
}, initAction : function() {
eventClick();
}
}
}();
但我不认为我的代码存在问题,因为它在其他计算机上运行良好。