为什么 node.js 在尝试在本地主机上运行应用程序时需要升级? [英] Why node.js requires an upgrade while trying to run an application on the localhost?

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

问题描述

当我尝试在 localhost 服务器上运行我的 node.js 应用程序时,它无法运行并需要进行必要的升级.我试图运行代码,但出现以下错误:

服务器代码

 var WebSocketServer = require('ws').Server,ws = new WebSocketServer({port: 80}),客户=[];**新的连接建立**ws.on('connection', function(conn) {CLIENTS.push(conn);conn.on('message', function(message) {console.log('收到:%s', message);发送所有(消息);});console.log("新连接");conn.send("新客户端连接");**如果你关闭连接**conn.on('关闭', function() {console.log("连接关闭");CLIENTS.splice(CLIENTS.indexOf(conn), 1);});});**向所有客户端发送消息**函数 sendAll(消息){for (var i=0; i

客户端代码

 

结果:<output name="" type="text" id="result" value"readonly"></output></p><input type="text" onchange="ws.send(this.value);"><脚本>var ws =new WebSocket('ws://localhost:80');ws.onmessage=函数(事件){document.getElementById("result").value=event.data;}

解决方案

Upgrade Required 是对在客户端(即浏览器)和客户端之间建立 WebSocket 连接时发送的标头的引用服务器.

就像@Prinzhorn 在他的评论中所说的那样,您需要一个连接到您的 WebSockets 服务器的客户端应用程序,它可以是一个静态 html 页面.我建议您阅读这篇 websockets 简介,以更好地了解 WebSockets 的工作原理.>

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:

server code

 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);
    }
}

client code

      <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>

解决方案

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.

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 在尝试在本地主机上运行应用程序时需要升级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆