2

newbie here, I need help with a Chat integration:

Here is a Function which I use to send message:

 ZegoExpressManager.shared.sendBroadcastMessage(
          config.roomID,
          "hello world!"
        );

With this one I receive it to my ConsoleLog:

 ZegoExpressManager.shared.onIMRecvBroadcastMessage(
        async (roomID, messageInfo) => {
        console.log("onIMRecvBroadcastMessage", roomID, messageInfo);}

ConsoleOutput:

{"time":"2022/07/09 12:34:40.611","level":1,"action":"zm.lrh.hsp","content":"push {\"header\":{\"sub_cmd\":\"/lr/push/im_chat\",\"msg_id\":\"1381362731MBy20xcWHa212374460\",\"send_time\":\"1657362880492\",\"room_id\":\"MBy20xcWHa\",\"sender_user_nid\":\"12443410347985956212\"},\"body\":{\"msg_data\":[{\"id_name\":\"935384446\",\"nick_name\":\"web-935384446\",\"msg_id\":\"31\",\"msg_category\":1,\"msg_type\":1,\"msg_priority\":1,\"msg_content\":\"\\hello world!\",\"send_time\":\"1657362880490\"}],\"room_id\":\"MBy20xcWHa\",\"server_msg_id\":\"31\",\"ret_msg_id\":\"31\"}}","appid":1381362731,"roomid":"MBy20xcWHa","userid":"719676606","userName":"","sessionid":""}

Now i need to extract "msg_content" and write it into a div

Thank You !

Here is Console Output as Text

{"time":"2022/07/09 12:34:40.611","level":1,"action":"zm.lrh.hsp","content":"push {"header":{"sub_cmd":"/lr/push/im_chat","msg_id":"1381362731MBy20xcWHa212374460","send_time":"1657362880492","room_id":"MBy20xcWHa","sender_user_nid":"12443410347985956212"},"body":{"msg_data":[{"id_name":"935384446","nick_name":"web-935384446","msg_id":"31","msg_category":1,"msg_type":1,"msg_priority":1,"msg_content":"\hello+world","send_time":"1657362880490"}],"room_id":"MBy20xcWHa","server_msg_id":"31","ret_msg_id":"31"}}","appid":1381362731,"roomid":"MBy20xcWHa","userid":"719676606","userName":"","sessionid":""}

THIS is What im Trying

JSON.parse()

  const message = JSON.parse(messageInfo);
      document.write(message.msg_content);

Output

[{"fromUser":{"userID":"72219676606","userName":"web-72219676606"},"message":"\nhallooo","sendTime":1657368483227,"messageID":55}]

@uminder 's Solution ends with following stacktrace

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'substring') at call.php?userID=9985SAS720&roomID=MZ9gK86EYN:266:40 at ZegoExpressManager.js:2:1299729 at Array.forEach () at e.actionListener (ZegoExpressManager.js:2:1299702) at e.handlePushRoomMsg (ZegoExpressManager.js:2:824050) at e.h [as handlePushRoomMsg] (ZegoExpressManager.js:2:814651) at e.handleSwitchPush (ZegoExpressManager.js:2:1334477) at e.handlePush (ZegoExpressManager.js:2:1333077) at ZegoExpressManager.js:2:1330411 at ZegoExpressManager.js:2:1139037 (anonymous) @ call.php?userID=9985SAS720&roomID=MZ9gK86EYN:266 (anonymous) @ ZegoExpressManager.js:2 e.actionListener @ ZegoExpressManager.js:2 e.handlePushRoomMsg @ ZegoExpressManager.js:2 h @ ZegoExpressManager.js:2 e.handleSwitchPush @ ZegoExpressManager.js:2 e.handlePush @ ZegoExpressManager.js:2 (anonymous) @ ZegoExpressManager.js:2 (anonymous) @ ZegoExpressManager.js:2 setTimeout (async) (anonymous) @ ZegoExpressManager.js:2 e.emit @ ZegoExpressManager.js:2 e.onPushEvent @ ZegoExpressManager.js:2 e.onPushEvent @ ZegoExpressManager.js:2 onPushEvent @ ZegoExpressManager.js:2 socketService.onMessage @ ZegoExpressManager.js:2 socket.socket.onmessage @ ZegoExpressManager.js:2

Larry
  • 21
  • 6

1 Answers1

1

You'll first have to extract, normalize and parse the relevant nested JSON.

const content = messageInfo.content;
const contentJSON = JSON.parse(content.substring(content.indexOf('{')).replace(/\\/g, ''));

Then, you can access the msg_content. Since msg_data is an array, you may find several of them.

const msgContent = contentJSON.body.msg_data.map(v => v.msg_content).join('\n');

Finally you assign the result to the innerText of the div.

document.querySelector('#msgContent').innerText = msgContent;

Please take a look at below code and see how it works.

const messageInfo = {
  "time": "2022/07/09 12:34:40.611",
  "level": 1,
  "action": "zm.lrh.hsp",
  "content": "push {\"header\":{\"sub_cmd\":\"/lr/push/im_chat\",\"msg_id\":\"1381362731MBy20xcWHa212374460\",\"send_time\":\"1657362880492\",\"room_id\":\"MBy20xcWHa\",\"sender_user_nid\":\"12443410347985956212\"},\"body\":{\"msg_data\":[{\"id_name\":\"935384446\",\"nick_name\":\"web-935384446\",\"msg_id\":\"31\",\"msg_category\":1,\"msg_type\":1,\"msg_priority\":1,\"msg_content\":\"\\This+is+my+message\",\"send_time\":\"1657362880490\"}],\"room_id\":\"MBy20xcWHa\",\"server_msg_id\":\"31\",\"ret_msg_id\":\"31\"}}",
  "appid": 1381362731,
  "roomid": "MBy20xcWHa",
  "userid": "719676606",
  "userName": "",
  "sessionid": ""
};

const content = messageInfo.content;
const contentJSON = JSON.parse(content.substring(content.indexOf('{')).replace(/\\/g, ''));
const msgContent = contentJSON.body.msg_data.map(v => v.msg_content).join('\n');
document.querySelector('#msgContent').innerText = msgContent;
<div id="msgContent"><div>
uminder
  • 23,831
  • 5
  • 37
  • 72
  • thank you for your Time: i got an **error [Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'substring')] ... >>>Stacktrace is up on Edit** – Newbie Net Doc Jul 09 '22 at 13:36