简体   繁体   English

通过websocket将字节数组从c#发送到angular

[英]Send byte array from c# to angular through websocket

Hi I would like to implement a function that a byte array is sent from C# to my Angular 7 through websocket.嗨,我想实现一个函数,通过 websocket 将字节数组从 C# 发送到我的 Angular 7。 Basically, I have a websocket running with C# and my frontend is written in Angular7.基本上,我有一个使用 C# 运行的 websocket,我的前端是用 Angular7 编写的。

Please note, the following example is a simplified one.请注意,以下示例是一个简化示例。 In my real application, the object I am going to send includes about 400 fields.在我的实际应用程序中,我要发送的对象包括大约 400 个字段。 After serialise the object to json string, it is about 6kb.将对象序列化为json字符串后,大约6kb。 In addition, I need to send 30 such objects in about one second.此外,我需要在大约一秒钟内发送 30 个这样的对象。 Sending binary data will significantly reduce the package size and speed it up.发送二进制数据将显着减小包大小并加快速度。

In C#, I have such example code to generate the payload of the websocket:在 C# 中,我有这样的示例代码来生成 websocket 的有效负载:

public class Test
{
    public int Id { get; set; }
    public float Value { get; set; }
    public string Description { get; set; }
}

public class Payload
{
    public object Obj { get; set; }
}

Test[] tests = new Test[]
{
    new Test
    {
         Id = 0,
         Value = 1.12f,
         Description = "The First Test"
    }
};


    byte[] testsByteArray;
    using (MemoryStream m = new MemoryStream())
    {
        using (BinaryWriter binaryWriter = new BinaryWriter(m))
        {
            foreach (Test test in tests)
            {
                binaryWriter.Write(test.Id);
                binaryWriter.Write(test.Value);
                binaryWriter.Write(test.Description);
            }
        }
    testsByteArray = m.ToArray();
}

Payload payload = new Payload
{
    Obj = testsByteArray
};

string a = JsonConvert.SerializeObject(payload);

Eventually I got a as {"Obj":"AAAAAClcjz8OVGhlIEZpcnN0IFRlc3Q="} I guess the value of Obj is base64 encoded value of testsByteArray .最终我得到了a as {"Obj":"AAAAAClcjz8OVGhlIEZpcnN0IFRlc3Q="}我猜Obj值是testsByteArray base64 编码值。

Now, in my frontend, I can receive the json string.现在,在我的前端,我可以接收 json 字符串。 The question is how to convert AAAAAClcjz8OVGhlIEZpcnN0IFRlc3Q= back to an object with the same format as Test in Angular.问题是如何将AAAAAClcjz8OVGhlIEZpcnN0IFRlc3Q=转换回与 Angular 中的Test具有相同格式的对象。

What I tried:我试过的:

I tried to use the following function atob() to decode the base64, and then use the following function to convert the decoded string to byte array我尝试使用以下函数atob()对base64进行解码,然后使用以下函数将解码后的字符串转换为字节数组

str2ab(str): ArrayBuffer {
    var buf: ArrayBuffer = new ArrayBuffer(str.length * 2); // 2 bytes for each char
    var bufView = new Uint16Array(buf);
    for (var i = 0, strLen = str.length; i < strLen; i++) {
      bufView[i] = str.charCodeAt(i);
    }
    return buf;
}

Then I try to use the following code to get the values然后我尝试使用以下代码来获取值

let dataView = new DataView(arrayBuffer);
console.log(dataView.getInt32(0));  // = 0
console.log(dataView.getInt32(2));  // = 0
console.log(dataView.getInt32(4));  // = 0
console.log(dataView.getInt32(6));  // = 10496

I am not exactly sure how to convert to correct value.我不确定如何转换为正确的值。 Can anyone help out.任何人都可以帮忙。

Thank you谢谢

Sending binary data between different platforms and languages is fragile and difficult to get right.在不同平台和语言之间发送二进制数据是脆弱的,很难做到正确。 I'd suggest converting to a platform agnostic representation (such as a JSON array) before sending, and convert back on the other side.我建议在发送之前转换为与平台无关的表示(例如 JSON 数组),然后在另一端转换回来。

For example,例如,

string a = JsonConvert.SerializeObject(tests);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM