【问题标题】:what should I put in the webSocket url?我应该在 webSocket url 中添加什么?
【发布时间】:2021-06-30 15:43:41
【问题描述】:

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

失败:连接建立错误: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);

【问题讨论】:

    标签: javascript node.js websocket electron ws


    【解决方案1】:

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

    【讨论】:

    • 我已经在使用 ws://129.0.0.1:5000。我认为问题出在服务器上
    • 我认为你在 127 和 129 之间有一点错别字..
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多