0

I am creating a chat application for my company, I am facing an issue that Web Socket is closed or already in closing state, whenever I try to connect. But the scenario is that the same source code same application is working fine on my friends laptop even its working fine on my office laptop, But in my home laptop it's giving me this error, I don't know that what's happening.

here is my ChatSocket.js code

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();
    }       
}
}();

But I don't think that my code have issue, because it is running well on other computers.

Al-un
  • 3,102
  • 2
  • 21
  • 40
Muhammad Yawar
  • 424
  • 1
  • 3
  • 19
  • 1
    You're not actually logging the error if it fails to connect to the socket, you're just logging the word "error", maybe your home laptop fails connecting to localhost:8086? – James Hunt Sep 15 '17 at 10:44
  • it connects to localhost:8086, because my website is deployed on that, and i am accessing it – Muhammad Yawar Sep 15 '17 at 10:45
  • actually it creates the connection and disconnects on the same time, one of my friend told me that you have firewall issue – Muhammad Yawar Sep 15 '17 at 10:48
  • Could you provide the log output, it may highlight which line is in error? Also, could you provide some server code extract? Maybe the server has a misconfiguration – Al-un Sep 16 '17 at 10:32
  • it was some browser or windows issue maybe, I have re installed windows and everything is working fine now – Muhammad Yawar Sep 16 '17 at 17:12

0 Answers0