The simulator of wechat applets, the real machine and the alipay applets received data parsing is normal, but the real machine of Alipay applets reported AMQJS0007E Socket error: failed to complete the operation. (OSStatus error -9807.) .

conclusion

Alipay applet returns data in Base64 format with line breaks. So there are errors when parsing. ** Solution: Remove newlines from ** strings.

data = data.replace(/[\r\n]/g,"");
Copy the code

The phenomenon of

The tester feedback said there was no track on the alipay mini program. And he’s like, “No way. Wechat is good, and the simulator is self-tested. There is no problem. There is something wrong with viewing the phenomenon. Nothing is displayed. Because tracks are pushed using MQTT, there is no way to capture and view data through Charles or view logs through the console. Also unlike wechat applet can open the debug template. As an uneducated person, I can only judge whether something is wrong by the contents of toast. ! _! If you have a good plan, please give me more advice.

MQTT does not support alipay small program real machine?

The first reaction is not to support it? Open mqtt.js and take a look. Predecessors have prepared compatible alipay small procedures, so that is to say that the program is no problem ah. Start adding logs and print out the error messages. Where MQTT is used, all failures and catches are logged and printed.

AMQJS0005E Internal error. Error Message: AMQJS0009E malformed UTF data:93 -3d.,Stack trace: No Error Stack Available

Base64 format problem?

After seeing the error log, asked Baidu “what reason ah, Baidu.” “Look at the ads first, or I won’t tell you.” I saw some ads and said, “Is it in Chinese?” It’s possible. Find the background developer, provide the login mobile phone number, help print the text is sent what. Thousands of hardships to find ah ah, said that after logging off immediately, did not send content. This is… So up and down. Again, there is no code to print the log. (Who knows)

Look for the error location

Take a look at all the code in the MQTT and add a log for each error branch along the error call stack. When the smallest block of code is viewed, a try-catch is performed on each line to find the specified line.

var ERROR = {
      OK: { code: 0, text: "AMQJSC0000I OK." },
      CONNECT_TIMEOUT: { code: 1, text: "AMQJSC0001E Connect timed out." },
      SUBSCRIBE_TIMEOUT: { code: 2, text: "AMQJS0002E Subscribe timed out." },
      UNSUBSCRIBE_TIMEOUT: { code: 3, text: "AMQJS0003E Unsubscribe timed out." },
      PING_TIMEOUT: { code: 4, text: "AMQJS0004E Ping timed out." },
      INTERNAL_ERROR: { code: 5, text: "AMQJS0005E Internal error. Error Message: {0}, Stack trace: {1}" },
      CONNACK_RETURNCODE: { code: 6, text: "AMQJS0006E Bad Connack return code:{0} {1}." },
      SOCKET_ERROR: { code: 7, text: "AMQJS0007E Socket error:{0}." },
      SOCKET_CLOSE: { code: 8, text: "AMQJS0008I Socket closed." },
      MALFORMED_UTF: { code: 9, text: "AMQJS0009E Malformed UTF data:{0} {1} {2}." },
      UNSUPPORTED: { code: 10, text: "AMQJS0010E {0} is not supported by this browser." },
      INVALID_STATE: { code: 11, text: "AMQJS0011E Invalid state {0}." },
      INVALID_TYPE: { code: 12, text: "AMQJS0012E Invalid type {0} for {1}." },
      INVALID_ARGUMENT: { code: 13, text: "AMQJS0013E Invalid argument {0} for {1}." },
      UNSUPPORTED_OPERATION: { code: 14, text: "AMQJS0014E Unsupported operation." },
      INVALID_STORED_DATA: { code: 15, text: "AMQJS0015E Invalid data in local storage key={0} value={1}." },
      INVALID_MQTT_MESSAGE_TYPE: { code: 16, text: "AMQJS0016E Invalid MQTT message type {0}." },
      MALFORMED_UNICODE: { code: 17, text: "AMQJS0017E Malformed Unicode string:{0} {1}." },
      BUFFER_FULL: { code: 18, text: "AMQJS0018E Message buffer is full, maximum buffer size: {0}." },
    };
Copy the code

The first thing you see is the source of the error. The original error log was reported by MQTT. The code for the error is

function parseUTF8(input, offset, length) {
      var output = "";
      var utf16;
      var pos = offset;

      while (pos < offset + length) {
        var byte1 = input[pos++];
        if (byte1 < 128)
          utf16 = byte1;
        else {
          var byte2 = input[pos++] - 128;
          if (byte2 < 0)
            throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), ""]));
          if (byte1 < 0xE0) // 2 byte character
            utf16 = 64 * (byte1 - 0xC0) + byte2;
          else {
            var byte3 = input[pos++] - 128;
            if (byte3 < 0)
              throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16)]));
            if (byte1 < 0xF0) // 3 byte character
              utf16 = 4096 * (byte1 - 0xE0) + 64 * byte2 + byte3;
            else {
              var byte4 = input[pos++] - 128;
              if (byte4 < 0)
                throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)]));
              if (byte1 < 0xF8) // 4 byte character
                utf16 = 262144 * (byte1 - 0xF0) + 4096 * byte2 + 64 * byte3 + byte4;
              else // longer encodings are not supported
                throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)]));
            }
          }
        }

        if (utf16 > 0xFFFF) // 4 byte character - express as a surrogate pair
        {
          utf16 -= 0x10000;
          output += String.fromCharCode(0xD800 + (utf16 >> 10)); // lead character
          utf16 = 0xDC00 + (utf16 & 0x3FF); // trail character
        }
        output += String.fromCharCode(utf16);
      }
      return output;
    }
Copy the code

Throw new Error(format(error.malFormed_utf, [byte1.toString(16), byte2.tostring (16), “”])); An exception was thrown. It can be determined precisely because after Base64 is transferred to ArrayBuffer, the judgment of data content is wrong.

Analysis ArrayBuffer

To determine if there is a problem with the data, print out the data from both the simulator and the real machine and compare them.If values fixed in the same place are found to be inconsistent, then some characters must have been added or removed.

View the Base64 string

Then look up the small program directly returned the string is there a problem, if there is a problem, then it must be the background for the real machine to do special processing! Or the real machine arm platform has different processing logic for a method! .It is also normal to look at Base64 strings, which are garbled when decoded in the Base64 standard format. Oh, that’s exciting. That must be the problem. Excitedly looking for background developers, must be your data problems. But… The slap in the face came fast. From the background log, you can see that the returned data is the same, regardless of platform. After checking the string of base64 again, I found that the “L” and “I” were written incorrectly. One is a lowercase L and one is a capital I. You can verify that the base64 sent from the background is correct as well. There is only one truth: Base64 to ArrayBuffer failed.

Turn Base64 ArrayBuffer

Found that wechat applet is to provide methods for base64 to ArrayBuffer, Base64 to string and so on. But pay treasure small program however what all have no, be I did not find!! (know, please add a comment, let me mobai below) looking for a long time also did not find what reliable, finally the curve to save the country. First convert Base64 to string, and then string to ArrayBuffer. It failed.

guess

If there is a problem with the real machine and the emulator, using the same string will also cause problems. Hard code the data directly. Then the simulator and the real machine test. Find… Pleasantly surprised. It all worked. No errors. 666 We can conclude that the string returned by the real machine must have special characters that are not visible. Spaces and newlines immediately come to mind.

successful

Once you strip the string of newlines and whitespace, everything is perfect. The last few tests found too many newlines. ^ _ ^ will mqtt.js code have a look, or receive a lot of ah!

// END hundreds of compilations and debugging a day and a night. Very sense of achievement!!