首页 > 解决方案 > 如何使用 node.js 和 web 套接字在终端上显示客户端的数据

问题描述

const WebSocket = require("ws");

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

wss.on("connection", (ws) => {
  console.log("New client connected!");

  ws.on("message", (data) => {
    console.log("Client has sent us: ${data}");
  });

  //ws.send(data.toUpperCase());

  ws.on("close", () => {
    console.log("Client has disconnected!");
  });
});

在这里,消息显示在本地主机下的开发工具网络面板上。但是,在节点终端中,它显示为 Client has sent us: ${data} 而预期的结果是 Client 已经向我们发送了:无论客户端发送的消息。我将附上终端的屏幕截图。提前致谢。终端图片

标签: node.jswebsocketserverclientmessage

解决方案


推荐阅读