gpt4 book ai didi

Make node display text after console.log() instead of (使节点在console.log()之后显示文本,而不是)

转载 作者:bug小助手 更新时间:2023-10-25 13:07:08 25 4
gpt4 key购买 nike



No matter what I do, node is displaying my console.log() text as a Buffer. Issue happens only in VSC powershell integrated terminal, in the browser message is displayed correctly.

无论我做什么,node都会将我的console.log()文本显示为缓冲区。只有在VSC PowerShell集成终端出现问题时,浏览器消息才会正确显示。


client.html

Client.html


<html>
<head>
<meta charset="UTF-8">
</head>
<script>
//calling the constructor which gives us the websocket object: ws
let ws = new WebSocket('ws://localhost:8000');
console.log(ws);
ws.onopen = (event) => ws.send("This is a message from client");
ws.onmessage = (message) => console.log(message.data.toString('utf8'))
</script>

<body>
<h1>This is a client page</h1>
</body>
</html>

server.js

Server.js


const http = require("http");
const websocket = require("ws");

//creating a http server
const server = http.createServer((req, res) => {
res.end("I am connected");
});

const wss = new websocket.Server({ server });

wss.on("connection", (ws, req) => {
setTimeout(() => {
ws.send("This is a message from server, connection is established");
}, 3000);
ws.on("message", (msg) => {
console.log(msg.toString('utf-8'));
});
});
//making it listen to port 8000
server.listen(8000);

I tried to access [data] property of WebSocket message, I also tried to use .toString('utf-8') with no success.

我尝试访问WebSocket消息的[Data]属性,也尝试使用.toString(‘utf-8’),但没有成功。


My Node version is v18.13.0

我的节点版本是v18.13.0


更多回答
优秀答案推荐

I found the issue.

我发现了问题所在。


in server.js file I wrote:

在server.js文件中,我写道:


  ws.on("message", (msg) => {
console.log(msg.toString('utf-8'));
});

I should write utf8 without -

我应该在写UTF8的时候-


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com