Bur*_*ney 2 express raspberry-pi typescript
在我的小项目中,我想构建一个可以使用 Raspberry Pi Zero 从任何网络浏览器访问的 BabyCam。为此,我想使用 express-is 打开一个网络套接字以将视频流式传输到多个客户端。我将大部分与视频相关的代码都基于raspivid-stream 模块。但是,当尝试访问 Web 套接字时,我收到了一个类型错误,用于app.ws(...说明Property 'ws' does not exist on type 'Application'. 当然,我导入了 express 和 express-ws 的类型。
我不太确定,问题是什么,因为 JavaScript 中的相同调用似乎工作正常。这是代码 - 我很高兴得到任何帮助!
import express from 'express';
import { Request, Response } from 'express';
import fs from 'fs';
import https from 'https';
import http from 'http';
import raspividStream from 'raspivid-stream';
import expressWs from 'express-ws';
const server: express.Application = express();
const httpPort: number = 8080;
const httpsPort: number = 8443;
const sslCredentials = {
key: fs.readFileSync('../ssl/localhost.key', 'utf8'),
cert: fs.readFileSync('../ssl/localhost.cert', 'utf8')
};
// CREATE SERVER
const httpServer = http.createServer(server);
const httpsServer = https.createServer(sslCredentials, server);
expressWs(server, httpsServer);
// ROUTES
server.get('*', (req: Request, res:Response) => {
if (!req.secure) {
return res.redirect(`https://${req.hostname}:${httpsPort}${req.originalUrl}`);
}
res.sendFile(__dirname + '/client/index.html');
});
// WEBSOCKET
server.ws('/video-stream', (ws) => {
console.log('Client connected');
ws.send(JSON.stringify({
action: 'init',
width: '960',
height: '540'
}));
var videoStream = raspividStream({ rotation: 180 });
videoStream.on('data', (data) => {
ws.send(data, { binary: true }, (error) => { if (error) console.error(error); });
});
ws.on('close', () => {
console.log('Client left');
videoStream.removeAllListeners('data');
});
});
// START SERVER
httpServer.listen(httpPort, () => {
console.log(`BabyCam (redirect) listening at http://localhost:${httpPort}/`);
});
httpsServer.listen(httpsPort, () => {
console.log(`BabyCam (SSL) listening at https://localhost:${httpsPort}/`);
});
Run Code Online (Sandbox Code Playgroud)
我看了一下https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express-ws/index.d.ts。
这就是(我认为)Stramski 的回答的意思:
import * as path from 'path';
import * as express from 'express';
import * as expressWs from 'express-ws';
const SERVER_PORT = 8090;
let appBase = express();
let wsInstance = expressWs(appBase);
let { app } = wsInstance; // let app = wsInstance.app;
// Http Routes
app.use('/assets', express.static(path.join(process.cwd(), 'assets')));
// Ws Routes
app.ws('/echotwice', (ws, req) => {
ws.on('message', msg => {
ws.send(msg + msg);
});
});
app.listen(SERVER_PORT, () => {
console.log(`App listening on port ${SERVER_PORT}!`);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1273 次 |
| 最近记录: |