套接字连接未通过javascript在https上建立

时间:2017-02-18 14:45:01

标签: javascript https websocket

我为我的网站开发了聊天功能,在非ssl网站上,连接是使用javascript建立到服务器,但是当我在网站上配置ssl时,套接字没有连接到服务器,浏览器控制台ERR_Connection建立错误,错误代码为1006.请帮我解决这个问题,这里是附加的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>
&#13;
&#13;
&#13;

0 个答案:

没有答案