これら 3 つの関数が互いにどのように関連しているかを誰か説明できますか?ここで $ が行っている関数は何ですか? それには2つの定義がありますか?
socket.onopen = function(){
log("Welcome - status "+this.readyState);
};
function $(id){
return document.getElementById(id);
}
function log(msg){
$("log").innerHTML+="<br>"+msg;
}
これらの関数は、以下のクライアント コードで記述されます。
var ソケット;
function init() {
var host = "ws://localhost:12345/websocket/server.php";
try {
socket = new WebSocket(host);
log('WebSocket - status ' + socket.readyState);
socket.onopen = function (msg) {
log("Welcome - status " + this.readyState);
};
socket.onmessage = function (msg) {
log("Received: " + msg.data);
};
socket.onclose = function (msg) {
log("Disconnected - status " + this.readyState);
};
} catch (ex) {
log(ex);
}
$("msg").focus();
}
function send() {
var txt, msg;
txt = $("msg");
msg = txt.value;
if (!msg) {
alert("Message can not be empty");
return;
}
txt.value = "";
txt.focus();
try {
socket.send(msg);
log('Sent: ' + msg);
} catch (ex) {
log(ex);
}
}
function quit() {
log("Goodbye!");
socket.close();
socket = null;
} // Utilities function $(id){ return document.getElementById(id); } function log(msg){ $("log").innerHTML+="<br>"+msg; } function onkey(event){ if(event.keyCode==13){ send(); } }