0

I have develop the chat feature for my website, on the non-ssl website, the connection is establishing to the server using javascript, but when i configure the ssl on the website, the socket is not connecting to the server, with the error in the browser console ERR_Connection establishment, with the error code 1006. Please help me out in this matter, here is the attached code of javascript.

<script>
        var host = 'wss://{host_ip_here}:4242';
        var socket = null;
        var input = document.getElementById('input');
        var messages = document.getElementById('messages');
        var print = function (input) {
               var sender_user_image=document.getElementById('sender_profile_image').value;
               var sender_name=document.getElementById('sender_name').value;
                    $("#messages").append('<li class="right clearfix"><span class="chat-img pull-right"><img src="'+sender_user_image+'" class="img-circle"></span><div class="chat-body clearfix"><div class="header"><strong class="primary-font">  ' + sender_name+'</strong>' +
                            '<small class="pull-right text-muted"><i class="fa fa-clock-o"></i>Few Seconds Ago</small>' +
                      '</div>' +
                     '<p>'+input+'' +
                      '</p>' +
                      '</div></li>');
            return;
        };

    //Manges the keyup event
    $("#send_button").button().click(function(){

            var msg = input.value;
            if (!msg)
                return;
            try {
            var messages = document.getElementById('input').value;
            var conversation_id = document.getElementById('conversation_id').value;
            var receiver_id = document.getElementById('receiver_id').value;
            var user_id = document.getElementById('user_id').value;
                socket.send(JSON.stringify({
                      receiver_id :receiver_id,sender_id:user_id,conversation_id : conversation_id,message:messages}));
                input.value = '';
                input.focus();
            } catch (e) {
                console.log(e);
            }
                print(msg);
            return;
    });

    try {
        socket = new WebSocket(host);
        //Manages the open event within your client code
        socket.onopen = function () {
            var user_id = document.getElementById('user_id').value;
            socket.send("user_id="+user_id);
            return;
        };
        //Manages the message event within your client code
        socket.onerror=function(res){
            console.error(res);
        };
        socket.onmessage = function (response) {

        var reply=JSON.parse(response.data);
        var replier_image=document.getElementById('receiver_image').value;
                $("#messages").append('<li class="left clearfix"><span class="chat-img pull-left"><img src="'+replier_image+'" class="img-circle"></span><div class="chat-body clearfix"><div class="header"><strong class="primary-font">'+reply.user_name+'</strong>' +
                        '<small class="pull-right text-muted"><i class="fa fa-clock-o"></i>'+reply.sent_time+'</small>' +
                  '</div>' +
                 '<p>'+reply.message+'</p>' +
                  '</div></li>');

            return;
        };
        //Manages the close event within your client code
        socket.onclose = function () {

            return;
        };
    } catch (e) {
        console.log(e);
    }
</script>
Atif Javed
  • 11
  • 1
  • 5
  • are you using self certificates? – Hosar Feb 18 '17 at 15:16
  • no, godaddy authorized certificates – Atif Javed Feb 18 '17 at 15:17
  • have you verified that 4242 port is open?, if it works on normal http there is no reason to do not work on https. – Hosar Feb 18 '17 at 15:22
  • i have checked, its running fine – Atif Javed Feb 18 '17 at 15:26
  • Your code suggest you are creating a websocket on something like "wss:1.1.1.1:4242" ... Are you missing the // in "wss://1.1.1.1:4242" ? – Anders Carstensen Feb 18 '17 at 17:12
  • No, its not. i had used the "//" like you said i have used "wss://1.1.1.1:4242", just missed up after removing the website ip. sorry for that. help will be apreciated. Thanks – Atif Javed Feb 19 '17 at 09:10
  • http://stackoverflow.com/questions/19304157/getting-the-reason-why-websockets-closed – robertklep Feb 19 '17 at 09:15
  • @robertklep, I have read all of the reasons for the abnormal code error i.e 1006, I dont know the problem behind this still. – Atif Javed Feb 19 '17 at 10:38
  • Have you tried different browsers (to rule out browser specific settings, like plugins), different computers (to rule out firewall issue), different internet connections (to rule out internet service provider issues)? – Anders Carstensen Feb 19 '17 at 12:20
  • @AndersKellerCarstensenm, Yes do. I have checked in other computers too. but the same issue. – Atif Javed Feb 19 '17 at 14:56
  • Did you try posting the message against another secure websocket server, like [this](http://www.websocket.org/echo.html)? If that works, then it must be your websocket server that is failing. – Anders Carstensen Feb 19 '17 at 22:28
  • @AndersKellerCarstensen, I have checked on the website you have sent, its working fine and sending messages when i am using the ws, but now connecting and sending message when i am using the wss. – Atif Javed Feb 20 '17 at 15:58

0 Answers0