“需要升级”(ws包)带有节点的WebSockets

我想简单地使用与Node js的WebSockets。 我告诉你我的代码:

的index.html

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>WebSockets</title> </head> <body> <script type="text/javascript"> const WebSocket = require('ws'); var sock = new WebSocket("ws://localhost:5001",{perMessageDeflate:false}); sock.onopen = function(event){ alert('Socket connected successfully'); setTimeout(function(){ sock.send("Hey !"); },1000); }; sock.onmessage = function(event){ console.log(event); }; </script> </body> </html> 

index.js

 var server = require('ws').Server; var s = new server({ port:5001}); 

我安装了包“ws”来使用WebSockets。 我的第一个目标是简单的console.log东西。

但是,我有这个错误… “需要升级”与我的GET请求状态426。

(我一直在关注在线Tutoriel https://www.youtube.com/watch?v=TPLAd-j9tMs ),但我得到这个错误…

一个人应该记住,这个包的文档说:“注意:这个模块不能在浏览器中工作,文档中的客户端是在WebSocket通信中引用客户端angular色的后端。必须使用本地的WebSocket对象。“

所以它可能会解释这个问题,但在这种情况下如何使用“本地WebSocket对象”?

提前致谢 !