简体   繁体   English

通过网络套接字接收数据时 Blob 获取的问题

[英]Problems with Blob acquisition when receiving data through a web-socket

In the process of communicating with the server using a web socket, the data is handed over to the server to Json.在使用web socket与服务器通信的过程中,将数据交给服务器交给Json。 And in the process of receiving data through onmessage, the data came into Blob, not Json or string.而且在通过onmessage接收数据的过程中,数据进来的是Blob,不是Json,也不是string。 I couldn't find a way to change this Blob to Json, so I posted a question here.我找不到将此 Blob 更改为 Json 的方法,所以我在这里发布了一个问题。

Is there any way to receive the data in string or Json?有没有办法接收字符串或Json 中的数据? Or how to change Blob to string or Json.或者如何将 Blob 更改为字符串或 Json。

The server is written in c++ and sent as a string.服务器是用 C++ 编写的,并作为字符串发送。

useEffect(() => {
        //var socket = new WebSocket("ws://172.30.1.50:65432/websocket");
        const socket = new WebSocket("ws://ip:port/websocket"); //For convenience, I wrote down ip and port.
        socket.Type = "blob"

        socket.onopen = function (event) {
            socket.send(JSON.stringify({ 'CMD': 'test' }))
            console.log("connetion?");}
        
        socket.close
        socket.onmessage = function (event) {
             console.log((event.data))
    }, []})

在此处输入图像描述

The WebSocket protocol supports two kinds of data messages: text and binary. WebSocket 协议支持两种数据消息:文本和二进制。 When a text message is received by Javascript, it is deserialised as a string;当 Javascript 收到一条短信时,它被反序列化为一个字符串; when a binary message is received, it is deserialised as either an ArrayBuffer or a Blob, depending on the value of the websocket's binaryType property (the default is to return a Blob).当收到二进制消息时,它会反序列化为 ArrayBuffer 或 Blob,具体取决于 websocket 的binaryType属性的值(默认返回 Blob)。

The fact that you got a Blob indicates that the server sent a binary message.你得到一个 Blob 的事实表明服务器发送了一个二进制消息。 This is unusual for a JSON message, so either the server is very unusual, or the protocol is not based on JSON.这对于 JSON 消息来说是不寻常的,所以要么服务器非常不寻常,要么协议不是基于 JSON 的。

In any case, you can convert r data into a string by calling the async .text() method:在任何情况下,您都可以通过调用 async .text()方法将 r 数据转换为字符串:

let data = await event.data.text();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么尝试建立网络套接字时连接被拒绝? - Why is connection refused when trying to establish a web-socket? 是否可以识别收到的网络套接字请求? - Is it possible to identify web-socket request on receive? 客户端(JS-浏览器)和服务器(PHP)通过 Web-Socket 通过 IP 通信 - Client(JS-Browser) and Server(PHP) communication over IP through Web-Socket 当我的互联网连接不稳定时(对于消息传递应用程序),我应该使用网络套接字吗? - Should I use web-socket when my internet connectivity is unstable (for messaging app)? 如何在没有 web-socket 的情况下从服务器监听事件 - How to listen the event from server without web-socket 如何为“ws”Web-Socket 模块创建自定义事件? - How to create custom events for 'ws' Web-Socket module? 如何使用来自网络套接字的网络音频 API 流式传输音频块? - how to stream audio chunks using web audio API coming from web-socket? 从 Z3D7A7766E8CDEB1C1FF8508204C4EBZ 接收来自 Z3D7A7766E8CDEB1C1FF8508204C4EBZ 的 stream 时,Blob 视频 stream 未显示 - Blob video stream not showing on iOS when receiving a stream from socket.io (JavaScript and Node.js) socket.on()不接收数据 - socket.on() not receiving data 如何将网络套接字对象从我的服务同步到组件-angular 2 - How do i sync a web-socket object from my service to the component -angular 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM