Arduino WiFi-Client Read not working properly

Hi all,

I am trying to use Websocket to get the real-time data from cloud. The HTTP part is as following:

void connectWSS(){

    if (client.connect(url_cumulocity_test2, 443)) {
    Serial.println("Websocket port is opened!");
    // Make a HTTP request:
    client.println("GET /notification2/consumer/?token=eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJ0ZXN0MlN1YnNjcmliZXIiLCJ0b3BpYyI6ImVudjc3OTcwMy9yZWxub3RpZi90ZXN0U3Vic2NyaXB0aW9uNSIsImp0aSI6IjRhNzdiOTgyLWJkOGQtNGUyNS05NDljLWZlMmFmMTNiOGQ3YSIsImlhdCI6MTY1OTA5NzQxNCwiZXhwIjoxNjU5MTA0NjE0fQ.K29nh2n4HHvfIm-PFH4Ajqwdw7oQZDTLHzkR4J-_b2xajAEqd0B3YHc2mX35DWbx3lTZomyOFpKEs0s894RTQFutpp1nPdCmnG-cN9cKGvpDSO91-LJkUnBHMLtS48VBrJ901QlRKSIFFGe1KKe8F3qhecMdF4HMrM192OoZQSlhMNHfkO4RsgHMtSxJuScLsfMiBrP5RaisTuhp4yCFAdxG1JF4JkRaXMFfUWFYStK4hQkAj4AN-hom4pYTfwsyMiw2_fRdigFcgD7_dszmC7DwD8173DD8L_6_KQJdKmm50fd9-Y_CoObo0DWjjfD5jI0oAhMD2r9pKIQw_kqC1Q&consumer=test2Subscriber HTTP/1.1");
    client.println("Host: env779703.eu-latest.cumulocity.com");
    //client.println("Authorization: Basic bGl1eWluemU6bDExNjExNjExNg==");
    client.println("Sec-WebSocket-Key: q4xkcO32u266gldTuKaSOw==");
    client.println("Upgrade: websocket");
    client.println("Connection: Upgrade");
    client.println("Sec-WebSocket-Version: 13");
    client.println();
     }else{
      Serial.println("Connection fail!");
      }
    }

I put it on the set up() section.

On the loop() section, I wrote something as following:

 void loop() {
  //String msg="";
  
  // if there are incoming bytes available
  // from the server, read them and print them: 
   while (client.available()) {
   
   char c = client.read();
   Serial.print(c);
     
   }

In this case, it will print any incoming msg sent from server. What I have received is:

> {"self":"https://env779703.eu-latest.cumulocity.com/measurement/measurements/109895","time":"2022-07-29T14:31:32.500+02:00","id":"109895","source":{"self":"https://env779703.eu-latest.cumulocity.com/inventory/managedObjects/1825","id":"1825"},"type":"c8y_Acceleration"}⸮~ICOW1BBCeCyAAMAE=
> /env779703/measurements/1825 CREATE

As you can see there are messy codes ⸮~ for some reason. I don't know why it happened honestly.

However, I tried using it in python with websocket-client package and I got the following msg:

CJi0BBC8ByAAMAE=
/env779703/measurements/1825
CREATE
{"self":"https://env779703.eu-latest.cumulocity.com/measurement/measurements/72755","time":"2022-07-28T10:34:32.000+02:00","id":"72755","source":{"self":"https://env779703.eu-latest.cumulocity.com/inventory/managedObjects/1825","id":"1825"},"type":"c8y_SignalStrengthWifi","c8y_SignalStrengthWifi":{"rssi":{"unit":"dBm","value":-65.0}}}

The layout is different because we have processed it. The question is the one important part

"c8y_SignalStrengthWifi":{"rssi":{"unit":"dBm","value":-65.0}}

is missing while using WiFi client.read(). And I really have no clue about it.

I am wondering whether there is something wrong in the buffer?

It would be really appreciated if you can help me.

If you really received that string and you're not hiding parts from us the server is not acting conforming to the standard. The response must start with a HTTP 101 response code and some mandatory header values. As it doesn't in your case it must be some proprietary protocol which I don't know.

Thanks for replying me. But is that possible that the problem could lie on the client.read() buffer? Since we get too much msg from server within a very short time period?

Of course this is possible but as we haven't seen the complete code we must ignore that possibility as we simply assume perfect code for every part we don't see, there must be a reason we you hide the code from us.