node.js - socket.io 由于数据大小而断开连接
问题描述
在下面的代码中:
客户端应该开始向服务器发送很长的 Base64 字符串的图像,一个接一个地间隔很近。
一旦第一个emit
带有 Base64 字符串的发生,套接字就会断开连接。我尝试使用硬编码的 2 个字符的字符串,但没有发生这种情况。
我相信问题出在数据的大小上,或者当第二个和第三个等..发出时,套接字还没有完成第一个 Base64 字符串的发送。
const io = new Server(httpServer, {
cors: {
credentials: true,
origin: 'http://localhost:4200',
methods: ["GET", "POST"]
}
})
io.on('connection', function(socket) {
console.log('connected')
socket.on('newDataFromClient', async (newDataFromTheClient) => {
// will work only after I refresh the page after a login
})
socket.on('disconnect', (reason) => {
// the reason is "transport error"
console.log('disconnected due to = ', reason)
})
})
httpServer.listen(4200)
这是客户端代码:
let socket
// this is only executed once, on page load
function setup() {
fetch('/setup')
.then((response) => response.json())
.then((setup) => {
doSomething(setup)
socket = io('http://localhost:4200', {
withCredentials: true,
transports: ['websocket']
})
})
}
// this is executed repeatedly at close intervals
async function sendToServer(imageAsBase64) {
socket.emit('newDataFromClient', imageAsBase64)
}
我究竟做错了什么?
解决方案
推荐阅读
- php - 卷曲重定向给出错误的数据
- github - Github 不显示 SVG 中定义的字体
- wordpress - 将特定的 URL/域名指向 hostgator 托管的插件/子域
- php - CURL UPS API,准系统版本
- bash - 如何将特定字符串添加到文件的每一行的末尾,但是某些块的字符串会发生变化
- prolog - 如何限制序言中的深度结构
- html - 如何使模拟 HTML YouTube 页面上的点赞按钮变为蓝色?
- c - 如何在工具栏外使用 Win32 工具栏标准位图?
- c++ - 我可以告诉 clang-format 在新行中打开和关闭圆括号吗?
- .net - BitOperations.PopCount 是在 .NET 中计算 BitArray 基数的最佳方法吗?