【问题标题】:socket.io - How to display data from server.js in an HTML page?socket.io - 如何在 HTML 页面中显示来自 server.js 的数据?
【发布时间】:2020-12-13 12:04:45
【问题描述】:

如何在 html 页面中显示 server.js 中的数据(socket.client.conn.server.clientsCount 中的连接用户数)?

例如:从 server.js 到 chat.html

【问题讨论】:

    标签: html node.js websocket socket.io


    【解决方案1】:

    您可以从后端通过套接字发送它,然后在前端处理它:

    后端:

    socket.emit('count', socket.client.conn.server.clientsCount);
    

    前端:

    socket.on('count', c => { document.querySelector('#count').textContent = c });
    

    【讨论】:

    猜你喜欢
    • 2023-03-24
    • 2016-08-28
    • 2022-11-21
    • 2013-07-22
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    相关资源
    最近更新 更多