我试图用websocket制作一个产品表,用于"实时"响应,只是想了解一下,我试图将一个旧项目升级到这种方法,显然我没有做到,这就是为什么我在这里

我真的不知道这里的问题,所以我有点迷路了, idea 是用户输入3个输入(标题、价格、缩略图),然后使用websocket,它将在下面显示为一个使用 bootstrap 属性的表格,供所有用户实时查看.

以下是我目前的代码:

html

    <div class="container mt-3 bg-dark">
        <h1 class="text-center text-primary">Ingrese datos</h1>

        <form class="text-light">

            <div class="form-group">
                <label for="title"><b>Producto</b></label>
                <input id="title" type="text" name="title">
            </div>
        
            <div class="form-group">
                <label for="price"><b>Precio</b></label>
                <input id="price" type="number" name="price">
            </div>
        
            <div class="form-group">
                <label for="thumbnail"><b>Thumbnail</b></label>
                <input id="thumbnail" type="text" name="thumbnail">
            </div>
        
            <button id="send">Postear</button>
        
        </form>

        <h2 class="text-center text-danger">Historial</h2>

        <div id="output"></div>
    </div>

产品js

const socket = io();

let title = document.getElementById("title")
let price = document.getElementById("price")
let thumbnail = document.getElementById("thumbnail")
let btn = document.getElementById("send")
let output = document.getElementById("output")


btn.addEventListener("click", function(){
    socket.emit("products", {
        title: title.value,
        price: price.value,
        thumbnail: thumbnail.value
    });
});

socket.on("products", function (data) {
    output.innerHTML += `
    <table class="table table-dark">
                <tr class="text-warning">
                    <th>Title</th>
                    <th>Price</th>
                    <th>Thumbnail</th>
                </tr>
                    <tr>
                        <td>
                            ${data.title}
                        </td>
                        <td>
                            ${data.price}
                        </td>
                        <td>
                            ${data.thumbnail}
                        </td>
                    </tr>
            </table>
    `
})

服务器的js

const path = require("path")
const express = require("express")
const app = express()

app.set("port", process.env.PORT || 8080)

app.use(express.static(path.join(__dirname, "public")))

const server = app.listen(app.get("port"), ()=>{
    console.log("server on port", app.get("port"))
})

const SocketIO = require("socket.io")
const io = SocketIO(server)

io.on("connection", (socket)=>{
    console.log("new connection", socket.id)

    socket.on("products", (data) => {
        io.sockets.emit("products", data);
    })
})

现在的问题是,当我单击"发送"按钮时,它什么都不做,服务器给了我一个新的连接,就是这样,从今天的websocket开始,所以我假设问题是一些非常基本的事先道歉

推荐答案

问题可能是当从客户端接收product消息时,您在服务器上使用的发送方法,它可能已被弃用,因此请try 将其更改为其他方法.

Change this:

io.sockets.emit("products", data);

To:

socket.emit("products", data);

Or:

io.emit("products", data);

您应该使用套接字的本地版本.HTML文件中的io:

<script src="/socket.io/socket.io.js"></script>

有关更多信息,请查看Getting started guideSocket.IO emit cheatsheet.

Javascript相关问答推荐

使用JavaScript重命名对象数组中的键

如何访问Json返回的ASP.NET Core 6中的导航图像属性

如何判断属于多个元素的属性是否具有多个值之一

我试图实现用户验证的reduxstore 和操作中出了什么问题?

防止用户在selectizeInput中取消 Select 选项

Chrome是否忽略WebAuthentication userVerification设置?""

Angular 17—每当一个布尔变量变为真时触发循环轮询,只要它保持为真

阿波罗返回的数据错误,但在网络判断器中是正确的

在open shadow—root中匹配时,使用jQuery删除一个封闭的div类

Websocket错误—有一个或多个保留位开启:reserved1 = 1,reserved2 = 0,reserved3 = 0

如何使用子字符串在数组中搜索重复项

rxjs插入延迟数据

JavaScript是否有多个`unfined`?

回溯替代方式

Google脚本数组映射函数横向输出

如何用javascript更改元素的宽度和高度?

使用RxJS from Event和@ViewChild vs KeyUp事件和RxJS主题更改输入字段值

如何在FiRestore中的事务中使用getCountFromServer

如何在尚未创建的鼠标悬停事件上访问和着色div?

Firefox的绝对定位没有达到预期效果