简体   繁体   English

谁能解释以下代码

[英]Can anyone explain the following code

I am fairly new to javascript and know only the basic. 我对javascript相当陌生,只了解基本知识。 Can anyone explain the following code as in the flow in which it occurs once the init function is called? 调用init函数后,有人能解释下面的代码吗?

My Understanding of the following code is that once the init function is called sets a global variable output which is mapped to an HTML element with the id output. 我对以下代码的理解是,调用init函数后,将设置一个全局变量输出,该输出将映射到具有id输出的HTML元素。 Then it calls the testWebSocket javascript function . 然后,它调用testWebSocket javascript function This creates a WebSocket object. 这将创建一个WebSocket对象。 After this is the part which I don't understand completely. 在这之后的部分我将无法完全理解。

Correct me if my understanding of the below is correct. 如果我对以下内容的理解正确,请纠正我。

In the line websocket.open = function(evt) { onOpen(evt) }; 在一行websocket.open = function(evt) { onOpen(evt) }; ,the WebSocket object has an attribute named open which we are setting to whatever is being returned by ,WebSocket对象具有一个名为open的属性,我们将该属性设置为

function(evt) { onOpen(evt) }; .

This in return calls the onOpen javascript function. 反过来,这会调用onOpen javascript函数。

function onOpen(evt)  {
   writeToScreen("CONNECTED");doSend("WebSocket rocks");
}

This has a call to the onSend javascript function. 这可以调用onSend javascript函数。

function doSend(message) {
   writeToScreen("SENT: " + message);websocket.send(message);
} 

So my first question what is being set in the WebSocket object(websocket.open) ?? 所以我的第一个问题是在WebSocket object(websocket.open)设置了什么?

Second Question: 第二个问题:

Below is the sequence in which the testWebSocket() javascript function is executed. 以下是执行testWebSocket() javascript function的顺序。

websocket = new WebSocket(wsUri);        
websocket.onopen = function(evt) { 
  onOpen(evt) 
};
websocket.onclose = function(evt) { 
  onClose(evt) 
}; 
websocket.onmessage = function(evt) {
  onMessage(evt) 
};
websocket.onerror = function(evt) {
  onError(evt) 
};

When I run the below code and in case there is an error I only get an error printed in the browser. 当我运行以下代码并万一出现错误时,我只会在浏览器中打印错误。

So my question is that even though the attributes of the WebSocket object(websocket) open, close, onmessage, onerror are being set and in each, I am calling the writeToScreen function why are they not being set and what is going on the testWebSocket() javascript function . 所以我的问题是,即使正在设置WebSocket object(websocket) open, close, onmessage, onerror的属性WebSocket object(websocket) open, close, onmessage, onerror ,并且在每个属性中,我都在调用writeToScreen函数,为什么未设置它们以及testWebSocket() javascript function

function init() {
    output = document.getElementById("output");
    testWebSocket();
}

function testWebSocket() {
    websocket = new WebSocket(wsUri);
    websocket.onopen = function(evt) {
        onOpen(evt)
    };
    websocket.onclose = function(evt) {
        onClose(evt)
    };
    websocket.onmessage = function(evt) {
        onMessage(evt)
    };
    websocket.onerror = function(evt) {
        onError(evt)
    };
}

function onOpen(evt) {
    writeToScreen("CONNECTED");
    doSend("WebSocket rocks");
}

function onClose(evt) {
    writeToScreen("DISCONNECTED");
}

function onMessage(evt) {
    writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>');
    websocket.close();
}

function onError(evt) {
    writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}

function doSend(message) {
    writeToScreen("SENT: " + message);
    websocket.send(message);
}

function writeToScreen(message) {
    var pre = document.createElement("p");
    pre.style.wordWrap = "break-word";
    pre.innerHTML = message;
    output.appendChild(pre);
}

You are looking at event handlers . 您正在查看事件处理程序

The testWebSocket function tries to create a WebSocket connection to wsUri . testWebSocket函数尝试创建与wsUri的WebSocket连接。

Once that connection is opened successfully, the onOpen function is called. 成功打开该连接后,将onOpen函数。 Whenever the WebSocket connection receives a message the onMessage function is called and once the connection is closed onClose will execute. 每当WebSocket连接接收到一条消息时,就会调用onMessage函数,并且一旦关闭该连接,就会执行onClose

If any of that goes wrong, the onError function will execute. 如果有任何错误,将执行onError函数。

You are probably only seeing the error being displayed, because the connection cannot be established and thus, onError is the only thing that is being called. 您可能只看到显示的错误,因为无法建立连接,因此onError是唯一被调用的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM