我应该在 webSocket url 中放什么?

what should I put in the webSocket url?

我正在使用电子开发桌面聊天应用程序。为了让聊天室正常工作,我必须配置 websockets。我的问题是我不知道我应该在 const ws = new WebSocket("ws://129.0.0.1:5000"); 中放什么我尝试了很多东西但我总是得到像

这样的错误

failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

这是渲染器

const ws = new WebSocket("ws://129.0.0.1:5000");
const send_btn = document.querySelector('.send_button');
send_btn.addEventListener('click', send_data());

ws.addEventListener('open', function(event){
  ws.send('hello server');
  console.log("data sent");
});

function send_data(){
  console.log("in");
  ws.send(document.getElementById("input_text").value);

ws.on('message', function incoming(data){
  console.log(data);
})};


function quit(){
  window.close();
}

这是 server.js 代码:

const WebSocket = require('ws');

const PORT = 5000;
const wss = new WebSocket.Server({
  port: PORT
});

wss.on("connection", ws =>{
    ws.on('message', function incoming(message){
       console.log('received: ', message);
    });
    ws.send("I am sending you back!");
});

console.log("Server is liestening on port " + PORT);

您的服务器正在侦听本地主机上的端口 5000。 所以如果你想连接到服务器,你必须输入“ws://localhost:5000”或“ws://127.0.0.1:5000”

NVM,我的问题是 main.js。我忘了要求服务器。我只是添加

const server = require('./server.js');