2016-07-08 2 views
-2

Ich habe einen Web-Socket auf meiner Website, und jetzt alle meine jQuery-Code funktioniert nicht mehr.Websocket blockiert meine jQuery von der Arbeit

Ich weiß nicht, was das Problem ist. Wie kann ich das beheben?

Hier ist der vollständige Code jQuery:

$(document).ready(function(){ 
    var i = parseInt($('#counter').val(), 10); 

    $("#postcount").on("click", function(){ 
     var z = i ++; 
     $('#counter').val(z); 
     alert(z); 
    }); 
}); 

Websocket Skript

var socket; 
function init() { 
    var host = "ws://127.0.0.1:9000/echobot"; 
    try { 
     socket = new WebSocket(host); 
     //log('WebSocket - status '+socket.readyState); 
     log('1'); 
    } 
    catch(ex){ 
     log(ex); 
    } 
    $("msg").focus(); 
} 
function send(){ 
    var txt,msg; 
    txt = $("msg"); 
    msg = txt.value; 
    if(!msg) { 
     log("Message can not be empty"); 
     return; 
    } 
    txt.value=""; 
    txt.focus(); 
    try { 
     socket.send(msg); 
     log("<b>"+msg+"</b>"); 
    } catch(ex) { 
     log(ex); 
    } 
} 

// Utilities 
function $(id) { 
    return document.getElementById(id); 
} 
function log(msg) { 
    $("log").innerHTML = msg; 
} 
function onkey(event) { 
    if(event.keyCode==13) { 
     send(); 
    } 
} 

Startseite

<body onload="init()"> 
    <div id="log"></div> 
    <input id="msg" type="textbox" onkeypress="onkey(event)" 
            onchange="send()" onkeyup="send()"/> 
    <button onclick="send()">Send</button> 
    <br/> 

    <input id="counter" type="textbox" value="2"/> 
    <input type="button" id="postcount" class="postcount" value="Reconnect"/> 
</body> 
+1

Haben Sie die Konsole zumindest auf Fehler überprüft? –

+0

Ich konnte nicht finden, was das Problem von der Konsole ist @RoryMcCrossan – Frank

+0

was ist 'Funktion $ (ID)' –

Antwort

0

es voll Beispiel versuchen:

$(document).ready(function(){ 
    var i = parseInt($('#counter').val(), 10); 

    $("#postcount").on("click", function(){ 
     var z = i ++; 
     $('#counter').val(z); 
     alert(z); 
    }); 

    init(); 
}); 

var socket; 
function init() { 

    var SO = window.WebSocket || window.MozWebSocket; 

    var host = "ws://127.0.0.1:9000/echobot"; 
    try { 
     socket = new SO(host); 
     //log('WebSocket - status '+socket.readyState); 
     log('1'); 
    } 
    catch(ex){ 
     log(ex); 
    } 

    $("msg").trigger("focus"); 
} 
function send(){ 
    var txt,msg; 
    txt = $("#msg"); 
    msg = txt.val(""); 
    if(!msg) { 
     log("Message can not be empty"); 
     return; 
    } 
    txt.val(""); 
    txt.focus(); 
    try { 
     socket.send(msg); 
     log("<b>"+msg+"</b>"); 
    } catch(ex) { 
     log(ex); 
    } 
} 


function log(msg) { 
    $("#log").html(msg); 
} 
function onkey(event) { 
    if(event.keyCode==13) { 
     send(); 
    } 
} 
+0

Ich tat, aber immer noch nicht funktioniert @toto – Frank

Verwandte Themen