本文介绍了为什么node.js在尝试在localhost上运行应用程序时需要升级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在localhost服务器上运行我的node.js应用程序时,它不会运行并要求进行必要的升级。我试图运行代码但是我收到以下错误:

When I try to run my node.js application on a localhost server, it does not run and demands a required upgrade. I have tried to run the code but I get the following error:

服务器代码

 var WebSocketServer = require('ws').Server,
    ws = new WebSocketServer({port: 80}),
    CLIENTS=[];


**new connection etablished**
 ws.on('connection', function(conn) {
         CLIENTS.push(conn);
         conn.on('message', function(message) {
         console.log('received:  %s', message);
         sendAll(message);

    });


 console.log("new connection");
         conn.send("NOUVEAU CLIENT CONNECTE");

                **if you close connection**
         conn.on('close', function() {
           console.log("connection closed");
           CLIENTS.splice(CLIENTS.indexOf(conn), 1);
         });

    });
    **send messeages vers all clients**

function sendAll (message) {
    for (var i=0; i<CLIENTS.length; i++) {
      var j=i+1;
      CLIENTS[i].send("Message pour le client "+j+": "+message);
    }
}

客户代码

      <p>
        Result :<output name="" type="text" id="result" value"readonly"></output>
      </p>
      <input type="text" onchange="ws.send(this.value);">
      </body>
      <script>
          var ws =new WebSocket('ws://localhost:80');
          ws.onmessage=function(event){
              document.getElementById("result").value=event.data;
          }
      </script>


推荐答案

需要升级是对在客户端(即浏览器)和服务器之间建立WebSocket连接时发送的标头的引用。

Upgrade Required is a reference to the header that is sent when establishing a WebSocket connection between a client (i.e. the browser) and the server.

像@Prinzhorn所述他的评论,你需要一个连接到你的WebSockets服务器的客户端应用程序,它可以是一个静态的html页面。我建议您阅读的介绍,以更好地了解WebSockets的工作原理。

Like @Prinzhorn stated in his comment, you need a client application that connects to your WebSockets server, which could be a static html page. I recommend you reading this introduction to websockets to understand better how WebSockets work.

这篇关于为什么node.js在尝试在localhost上运行应用程序时需要升级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 21:21