WebSocket 中子协议支持

WebSocket提供了一种低级别的消息传递体系结构,允许应用程序在TCP上直接建立持久连接。虽然它不强制使用特定的消息协议,但需要额外的框架层来处理消息路由和解释。子协议如STOMP被用来增强WebSocket的功能,使得像Spring这样的框架能支持标准的消息传递,适合Web应用。

WebSocket 确实指定了一种消息传递体系结构,但并不强制使用任何特定的消息传递协议。而且它是 TCP 上的一个非常薄的层,它将字节流转换为消息流(文本或二进制)仅此而已。由应用程序来解释消息的含义。

与 HTTP(它是应用程序级协议)不同,在 WebSocket 协议中,传入消息中根本没有足够的信息供框架或容器知道如何路由或处理它。因此,对于非常琐碎的应用程序而言 WebSocket 协议的级别可以说太低了。

可以做到的是引导在其上面再创建一层框架。这就相当于当今大多数 Web 应用程序使用的是 Web 框架,而不直接仅使用 Servlet API 进行编码一样。

WebSocket RFC 定义了子协议的使用。在握手过程中,客户机和服务器可以使用头 Sec-WebSocket 协议商定子协议,即使不需要使用子协议,而是用更高的应用程序级协议,但应用程序仍需要选择客户端和服务器都可以理解的消息格式。且该格式可以是自定义的、特定于框架的或标准的消息传递协议。

例如:Spring 框架支持使用 STOMP,这是一个简单的消息传递协议,最初创建用于脚本语言,框架灵感来自 HTTP。STOMP 被广泛支持,非常适合在 WebSocket 和 web 上使用。

const set = require('./stream_set.json') console.log(set) const setInfo = set.data.map((item) => { let res = {} if (item.name) { res.type = 1 } else { res.type = 0 } res.duration = item.duration res.item = item res.settings = [] Object.entries(item).forEach((i) => { // console.log(i) if (i[1] != '' && i[0] != 'duration') { res.settings.push(i[1]) } }) return res }) console.log(setInfo) const folderPath = './data' const folderPath2 = `./data/${set.type}` // console.log(getBasenames(folderPath2)) let baseNames = getBasenames(folderPath2) console.log(baseNames) let folderSplitArr = baseNames.map((item) => { return [item, item.split('_').slice(0, -1)] //去除末尾编号和时长 }) console.log('folder:') console.log(folderSplitArr) function getBasenames(folderPath) { // 验证文件夹存在性 if (!fs.existsSync(folderPath)) { throw new Error(`文件夹不存在: ${folderPath}`) } const stats = fs.statSync(folderPath) if (!stats.isDirectory()) { throw new Error(`路径不是文件夹: ${folderPath}`) } const items = fs.readdirSync(folderPath, { withFileTypes: true }) const results = [] for (const item of items) { if (item.isFile()) { // 获取不含后缀的文件名 const basename = path.parse(item.name).name if (results.includes(basename)) { continue } results.push(basename) } } return results } function allElementsExist(arrA, arrB) { const setB = new Set(arrB) return arrA.every((item) => setB.has(item)) } function randomMatchFiles(set, folderSplitArr) { let timeLength = [] let data = [] set.forEach((item) => { let settings = item.settings let type = item.type if (type) { //指定名称 console.log(settings[0]) console.log(item.duration) if (baseNames.indexOf(settings[0]) !== -1) { data.push(settings[0]) timeLength.push(item.duration) } } else { const matches = [] folderSplitArr.map((arr) => { if (allElementsExist(settings, arr[1])) { matches.push(arr[0]) } }) console.log('matches:') console.log(matches) if (matches.length === 0) { console.log(`存在配置项无法找到匹配数据:`) console.log(item.item) // return } else { timeLength.push(item.duration) data.push(matches[Math.floor(Math.random() * matches.length)]) } } }) return [timeLength, data] } const [timeLength, data] = randomMatchFiles(setInfo, folderSplitArr) console.log(timeLength) console.log(data) const bufferAll = data.map((item) => `./data/${set.type}/${item}.buffer`) console.log(bufferAll) const jsonAll = data.map((item) => `./data/${set.type}/${item}.json`) const mockTime = timeLength.map((item) => item * 1000) console.log(mockTime) console.log(timeLength.length, data.length, set.data.length) const httpServe = require('./http') const wsServe = require('./websocket') if (set.protocol === 'http') { httpServe(mockTime, bufferAll, jsonAll) } else if (set.protocol === 'ws') { wsServe(mockTime, bufferAll, jsonAll) } else { } 根据上述代码生成mermaid图的代码
10-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值