polling-xhr.js:229 GET http://localhost:3000/socket.io/?EIO=4&transport=polling&t=NMtL4rR net::ERR_CONNECTION_REFUSED

cbr*_*bra 1 html javascript notifications

我正在尝试构建一个表单,在提交时发送包含填充数据的通知。当我运行代码时,它在控制台上显示此错误

polling-xhr.js:229 GET http://localhost:3000/socket.io/?EIO=4&transport=polling&t=NMtL4rR net::ERR_CONNECTION_REFUSED

这个错误看起来像是溢出

这是我的代码

管理.html

<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
   <link rel="stylesheet" 
  href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" 
  integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" 
  crossorigin="anonymous">

  <title>Hello, admin</title>
  </head>
  <body>
  <div class="container" style="max-width: 500px;">
    <div style="margin-top: 40px;"></div>
    <h1>Send Notification</h1>
    <form>
        <div class="form-group">
          <label for="name">name</label>
          <input type="text" class="form-control" id="name" aria-describedby="emailHelp">
        </div>
        <div class="form-group">
            <label for="profession">profession</label>
            <input type="text" class="form-control" id="profession" aria-describedby="emailHelp">
          </div>
          <div class="form-group">
            <label for="email">Email address</label>
            <input type="email" class="form-control" id="email" aria-describedby="emailHelp">
          </div>
       
       
        <button type="button" id="sendNotification" class="btn btn-primary">Submit</button>
      </form>
  </div>


 <!-- Optional JavaScript; choose one of the two! -->

 <!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
 <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384- 
 DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
 <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" 
   integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" 
   crossorigin="anonymous"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.1/socket.io.js"></script>

   <script type="text/javascript">
    var socket = io("http://localhost:3000");

    $( "#sendNotification" ).click(function() {

        var name = $( "#name" ).val()
        var profession = $( "#profession" ).val()
        var email = $( "#email" ).val()

        socket.emit("sendNotification" ,{
            "name" : name,
            "profession" : profession,
            "email" : email
        });
    });
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

用户.html

<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

 <!-- Bootstrap CSS -->
 <link rel="stylesheet" 
 href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" 
 integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" 
 crossorigin="anonymous">

 <title>Hello, user</title>
 </head>
 <body>
  <div class="container" style="max-width: 500px;">
    <div style="margin-top: 40px;"></div>
    <h1>Get Notification</h1>
    
  </div>



<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.1/socket.io.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/notify/0.4.2/notify.js"></script>


<script type="text/javascript">
    var socket = io("http://localhost:3000");

    socket.on("sendNotification", function(details){
        $.notify("Name :"+details.name, {
            autoHide : false,
            className : "success"
        });
    });

    
</script>

<!-- Option 2: jQuery, Popper.js, and Bootstrap JS
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384- 
DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384- 
9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" 
integrity="sha384-w1Q4orYjBQndcko6MimVbzY0tgp4pWB4lZ7lr30WKz0vr/aWKhXdBNmNb5D92v7s" 
crossorigin="anonymous"></script>
-->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

服务器.js

var express = require("express");
var app = express();

var http = require("http").createServer(app);
var io = require("socket.io")(http);

http.listen(3000, function(){
console.log("Successfully Connected Node Server");

 io.on("connection", function(socket){
    console.log("Auth value: " + socket.id);

    socket.on("sendNotification", function(details){
        socket.broadcast.emit("sendNotification", details);
    });
});
});
Run Code Online (Sandbox Code Playgroud)

包.json

 {
 "name": "notification-server",
 "version": "1.0.0",
 "description": "Notification",
 "main": "index.js",
 "scripts": {
"test": "none"
 },
"repository": {
"type": "git",
"url": "none"
 },
"keywords": [
"none"
],
"author": "cbra",
"license": "MIT",
"dependencies": {
"express": "^4.17.1",
"http": "0.0.1-security",
"socket.io": "^3.0.1"
}
}
Run Code Online (Sandbox Code Playgroud)

我必须在这里改变什么?

Gus*_*lli 5

我认为你需要一个CORS模块。有两种方法


  1. 使用 npm
npm install cors
Run Code Online (Sandbox Code Playgroud)
// server.js

...
app.use(require('cors')())
...
Run Code Online (Sandbox Code Playgroud)
  1. 启用 CORS 选项(如果您使用的是 Socket.IO v3)
const io = require("socket.io")(http, {
    cors: {
        origin: "http://localhost:4200",
        methods: ["GET", "POST"]
    }
})
http.listen(3000)
Run Code Online (Sandbox Code Playgroud)