直接从Uwebsockets服务器读取Uint8array
Read uint8array directly from uWebsockets server
我正在尝试使用uwebsockets库读取从Web客户端发送的二进制阵列数据。
我找到了一个有关回购问题的问题,该问题描述了这是针对文本/blob数据的情况,但是我想解析来自Web客户端的uint8array数据:
-
https://gist.github.com/westhorburn/581D8B7A0ED70BC386B8C38883336AD318
-
https://github.com/unetworking/uwebsockets/issues/508
我正在使用非常简单的示例客户端:
int main()
{
uWS::Hub h;
h.onMessage([](uWS::WebSocket<uWS::SERVER> *ws, char *message, size_t length, uWS::OpCode opCode) {
uint8_t messageString [length]; //Create char array with length of message length
cout << "length: " << length << endl;
for (int i = 0; i < length; i++) { //Iterate through each character in the message
messageString[i] = (uint8_t)message[i]; //Add message character to messageString
}
cout << "Message: " << &messageString[1] << endl;
ws->send (message, length, opCode);
});
h.listen (3000);
h.run();
}
在客户端我做:
{
ws = new WebSocket(wsUri);
ws.binaryType = "arraybuffer";
ws.onopen = function (evt) { onOpen(evt) };
ws.onclose = function (evt) { onClose(evt) };
ws.onmessage = function (evt) { onMessage(evt) };
ws.onerror = function (evt) { onError(evt) };
}
并将uint8array
向下发送插座。文本/斑点示例不起作用,试图将其读成uint8_t
阵列不起作用,我无法确定自己的一生读取如何读取此内容,以便数据可用。请注意,回声回到客户端确实有效 - 我拿回来了。
从客户端阅读数据并响应它们很简单:
h.onMessage([](uWS::WebSocket<uWS::SERVER> *ws, char *message, size_t, length, uWS::OpCode opCode) {
std::string receivedMessage(message, length);
ws->send("Reponse from server", uWS::OpCode::BINARY);
});
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 无法找到/读取配置文件.conf-FileIOException
- 如何使用Luacneneneba API正确读取字符串和表参数
- C++将文本文件中的数据读取到结构数组中
- 正在将csv文件读取为双精度矢量
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 为什么在读取文件大小时文件IO速度会发生变化
- 正在读取二进制文件(is_open)
- 如何在c++中从文本文件中逐行读取整数
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- 独立读取-修改-写入顺序
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 直接从Uwebsockets服务器读取Uint8array