0

I'm trying to follow this webRTC example, but am running into trouble on something that seems trivial... The if statement never returns true, even though printing the message to the console seems to show that property is in the JSON

function gotMessageFromServer(msg) {

    // returns error, so assume msg is already parsed? 
    // var signal = JSON.parse(msg); 
    var signal = msg;

    console.log(msg);
    console.log(msg["sdp"]);
    console.log(typeof(msg));

    if (signal.sdp) {

        // signal.sdp = false?

    }

Console Output:

{"sdp":{"type":"offer","sdp":"v=0\r\no=mozilla...THIS_IS_SDPARTA-45.0 2382924134299995518 0 IN IP4 0.0.0.0\r\ns=-\r\nt=0 0\r\na=sendrecv\r\na=fingerprint:sha-256 21:44:49:DD:C0:D9:CF:B6:EE:BB:69:FA:A6:65:F1:63:D1:E1:31:A0:6B:EE:A1:08:E4:36:47:B3:21:A3:3B:16\r\na=group:BUNDLE sdparta_0 sdparta_1\r\na=ice-options:trickle\r\na=msid-semantic:WMS *\r\nm=audio 9 UDP/TLS/RTP/SAVPF 109 9 0 8\r\nc=IN IP4 0.0.0.0\r\na=recvonly\r\na=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\na=ice-pwd:10d93ec1c51773af4e04dc11ade88d5e\r\na=ice-ufrag:54ca9275\r\na=mid:sdparta_0\r\na=rtcp-mux\r\na=rtpmap:109 opus/48000/2\r\na=rtpmap:9 G722/8000/1\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:8 PCMA/8000\r\na=setup:actpass\r\na=ssrc:232522377 cname:{bb5e63da-1ee9-4fc0-bb49-c96c58194b37}\r\nm=video 9 UDP/TLS/RTP/SAVPF 120 126 97\r\nc=IN IP4 0.0.0.0\r\na=recvonly\r\na=fmtp:120 max-fs=12288;max-fr=60\r\na=fmtp:126 profile-level-id=42e01f;level-asymmetry-allowed=1;packetization-mode=1\r\na=fmtp:97 profile-level-id=42e01f;level-asymmetry-allowed=1\r\na=ice-pwd:10d93ec1c51773af4e04dc11ade88d5e\r\na=ice-ufrag:54ca9275\r\na=mid:sdparta_1\r\na=rtcp-fb:120 nack\r\na=rtcp-fb:120 nack pli\r\na=rtcp-fb:120 ccm fir\r\na=rtcp-fb:126 nack\r\na=rtcp-fb:126 nack pli\r\na=rtcp-fb:126 ccm fir\r\na=rtcp-fb:97 nack\r\na=rtcp-fb:97 nack pli\r\na=rtcp-fb:97 ccm fir\r\na=rtcp-mux\r\na=rtpmap:120 VP8/90000\r\na=rtpmap:126 H264/90000\r\na=rtpmap:97 H264/90000\r\na=setup:actpass\r\na=ssrc:1196553112 cname:{bb5e63da-1ee9-4fc0-bb49-c96c58194b37}\r\n"},"uuid":"280a6161-5837-4d40-7752-a9e3d6688421"}

undefined

object
andy mcevoy
  • 378
  • 3
  • 12
  • 2
    What's `console.log(typeof msg)` say? – Quentin May 06 '16 at 06:38
  • @RobG — Sounds like you are converting the console output to a string by wrapping it in quotes but failing to escape the special characters inside it. Being parsed as a JS string literal without that escaping is breaking it. – Quentin May 06 '16 at 06:49
  • typeof gives object... – andy mcevoy May 06 '16 at 06:49
  • @andymcevoy — Then it definitely isn't a string of JSON. I can't see any reason why `msg["sdp"]` would be undefined though. – Quentin May 06 '16 at 06:50
  • Is the code you posted a simplified version of what your actually doing? Is there other things you omitted that you thought wasn't relevant? As @Quentin says that should work as its an actual JS object. – ste2425 May 06 '16 at 06:54
  • ok, that means its a stupid mistake that I just haven't found yet... – andy mcevoy May 06 '16 at 06:54
  • No, check out the link, my function is exactly the same with the console.log() statements added... but you're right something doesn't add up. – andy mcevoy May 06 '16 at 06:55
  • What's the error of JSON.parse? – aravind May 06 '16 at 06:56
  • Check `msg instanceof String` if true then no wonder `typeof msg` gives `object` while `msg.sdp` is undefined. – Yury Tarabanko May 06 '16 at 07:02
  • https://jsfiddle.net/x6uh9L7p/ demo – Yury Tarabanko May 06 '16 at 07:12
  • @YuryTarabanko — It might vary between browsers, but if it was a String object then I'd expect logging it to give something more like this: `String {0: "T", 1: "h", 2: "i", 3: "s", 4: " ", 5: "i", 6: "s", 7: " ", 8: "a", 9: " ", 10: "t", 11: "e", 12: "s", 13: "t", length: 14, [[PrimitiveValue]]: "This is a test"}` then what you would expect when logging a string primative. – Quentin May 06 '16 at 07:20
  • @Quentin true and `JSON.parse` wouldn't fail on Strings. @AndyMcevoy which browser are you using? – Yury Tarabanko May 06 '16 at 07:28
  • `msg` is an object that encapsulate your data message. Your data message seems to be in `msg.data`. So it should be `JSON.parse(msg.data)` https://github.com/shanet/WebRTC-Example/blob/master/client/webrtc.js#L53 – Luca Rainone May 06 '16 at 11:38

1 Answers1

-2

Check

console.log(typeof(msg))   

If it is string , you need to use JSON.parse(msg)

try

console.log(msg.sdp);

Update : Just checked (http://jsonlint.com/)

{"sdp":{"type":"offer","sdp":"v=0\r\no=mozilla...THIS_IS_SDPARTA-45.0 2382924134299995518 0 IN IP4 0.0.0.0\r\ns=-\r\nt=0 0\r\na=sendrecv\r\na=fingerprint:sha-256 21:44:49:DD:C0:D9:CF:B6:EE:BB:69:FA:A6:65:F1:63:D1:E1:31:A0:6B:EE:A1:08:E4:36:47:B3:21:A3:3B:16\r\na=group:BUNDLE sdparta_0 sdparta_1\r\na=ice-options:trickle\r\na=msid-semantic:WMS *\r\nm=audio 9 UDP/TLS/RTP/SAVPF 109 9 0 8\r\nc=IN IP4 0.0.0.0\r\na=recvonly\r\na=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\na=ice-pwd:10d93ec1c51773af4e04dc11ade88d5e\r\na=ice-ufrag:54ca9275\r\na=mid:sdparta_0\r\na=rtcp-mux\r\na=rtpmap:109 opus/48000/2\r\na=rtpmap:9 G722/8000/1\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:8 PCMA/8000\r\na=setup:actpass\r\na=ssrc:232522377 cname:{bb5e63da-1ee9-4fc0-bb49-c96c58194b37}\r\nm=video 9 UDP/TLS/RTP/SAVPF 120 126 97\r\nc=IN IP4 0.0.0.0\r\na=recvonly\r\na=fmtp:120 max-fs=12288;max-fr=60\r\na=fmtp:126 profile-level-id=42e01f;level-asymmetry-allowed=1;packetization-mode=1\r\na=fmtp:97 profile-level-id=42e01f;level-asymmetry-allowed=1\r\na=ice-pwd:10d93ec1c51773af4e04dc11ade88d5e\r\na=ice-ufrag:54ca9275\r\na=mid:sdparta_1\r\na=rtcp-fb:120 nack\r\na=rtcp-fb:120 nack pli\r\na=rtcp-fb:120 ccm fir\r\na=rtcp-fb:126 nack\r\na=rtcp-fb:126 nack pli\r\na=rtcp-fb:126 ccm fir\r\na=rtcp-fb:97 nack\r\na=rtcp-fb:97 nack pli\r\na=rtcp-fb:97 ccm fir\r\na=rtcp-mux\r\na=rtpmap:120 VP8/90000\r\na=rtpmap:126 H264/90000\r\na=rtpmap:97 H264/90000\r\na=setup:actpass\r\na=ssrc:1196553112 cname:{bb5e63da-1ee9-4fc0-bb49-c96c58194b37}\r\n"},"uuid":"280a6161-5837-4d40-7752-a9e3d6688421"} 

It is a valid JSON format.

It should work.

NeiL
  • 791
  • 8
  • 35
  • 3
    This debugging advice, *Check console.log(typeof(msg))*, is a comment rather than an answer. The OP says that *If it is string , you need to use JSON.parse(msg)* throws an error, so that can't be the solution. *console.log(msg.sdp);* means exactly the same as *console.log(msg["sdp"]);* which the OP said outputs `undefined`. – Quentin May 06 '16 at 06:41
  • @Quentin Let him check it , i'll update the answer accordingly – NeiL May 06 '16 at 06:46
  • 3
    If you want to make a comment with the intention of turning it into an answer once the OP provides enough information to answer the question … then *make a comment* and **then** give an answer when you have the information you need to give a real one. – Quentin May 06 '16 at 06:47