提问人:Jarrett Dunn 提问时间:11/8/2023 最后编辑:jQueenyJarrett Dunn 更新时间:11/9/2023 访问量:27
Node.js 销毁常量
Node.js destroying Constant
问:
我正在重新发明轮子,并使用树莓派,Node.js v18.18和Hub75 LED矩阵构建LED显示矩阵。有一个小的 Web 应用程序允许您输入文本并将其发送到矩阵进行显示。最初,它完全按预期工作。网站出现,您可以输入文本,然后显示。从输入第一条消息开始大约 10 秒后,将向控制台发送一条消息:
Server is running on port 8080
Received text: Hello
Destroying font
Received text: World
Web 服务器仍然有效,但文本不会显示在矩阵上。我假设这是因为字体被破坏了。我的信念是,随着套接字连接到期,它正在被破坏。但是,我无法弄清楚它为什么以及如何破坏正在使用并在套接字范围之外声明的常量。
我是node.js的新手,这是Python的重大转变。任何见解或帮助都非常感谢。
App.js
coconst express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const { Font, LedMatrix, LedMatrixUtils } = require('rpi-led-matrix');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
// Define your matrix configuration
const matrixOptions = {
...LedMatrix.defaultMatrixOptions(),
rows: 32,
cols: 64,
chainLength: 2,
parallel: 1,
brightness: 100,
hardwareMapping: 'Adafruit-hat-pwm',
};
// Font
const font = new Font('5x7', __dirname + '/node_modules/rpi-led-matrix/fonts/5x7.bdf');
const runtimeOptions = LedMatrix.defaultRuntimeOptions();
const matrix = new LedMatrix(matrixOptions, runtimeOptions);
matrix
.clear()
.brightness(100)
.fgColor(0x00adff)
.drawRect(0, 0, 63, 31)
.font(font)
.fgColor(0xff0000)
.drawText('Hello', 1, 1)
.drawText('World', 1, 8)
.sync();
app.use(express.static(__dirname ));
app.use(express.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.sendFile('/index.html');
});
app.post('/display', (req, res) => {
const text = req.body.text;
io.emit('display_text', text);
// Clear the matrix
matrix.clear();
});
io.on('connection', (socket) => {
socket.on('display_text', (text) => {
// You can control the matrix here
// For simplicity, we'll just log the text
console.log('Received text:', text);
matrix.clear();
matrix.drawText(text, 1, 1);
matrix.sync();
});
});
server.listen(8080, () => {
console.log('Server is running on port 8080');
});
索引.html
<!DOCTYPE html>
<html>
<head>
<title>LED Matrix Text Display</title>
</head>
<body>
<h1>LED Matrix Text Display</h1>
<form action="/display" method="POST">
<label for="text">Enter text:</label>
<input type="text" id="text" name="text">
<button type="submit">Display Text</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
document.querySelector('form').addEventListener('submit', (e) => {
e.preventDefault();
const text = document.querySelector('#text').value;
socket.emit('display_text', text);
});
</script>
</body>
</html>
答: 暂无答案
评论