www.webdeveloper.com
Results 1 to 4 of 4

Thread: socket.send Troubleshooting Javascript websocket send

Hybrid View

  1. #1
    Join Date
    Nov 2012
    Posts
    3

    socket.send Troubleshooting Javascript websocket send

    I am trying to get Javascript to send a command to the server.

    Code:
    {"op":"addr_sub", "addr":"address"}
    The original script I could just type that in to the form replace the address and hit send.

    Trying to get it so you can just enter the address and hit send.

    I modified the function send() by adding a var address. I set that equal to the input from the form; what used to be msg. And set msg to what I want the script to send the server.

    Code:
    function send(){
      var txt,msg,address;
      txt = $("msg");
      address = txt.value
      msg = ({"op":"addr_sub", "addr":"address"});
      if(!msg){ alert("Message can not be empty"); return; }
      txt.value="";
      txt.focus();
      try{ socket.send(msg); log('Sent: '+msg); } catch(ex){ log(ex); }
    }
    Im getting the following error.

    [Exception... "Component returned failure code: 0x80460001 (NS_ERROR_CANNOT_CONVERT_DATA)" nsresult: "0x80460001 (NS_ERROR_CANNOT_CONVERT_DATA)" location: "JS frame :: XXXXX.POS/POS.php :: send :: line 39" data: no]

    This is the original script

    Code:
    function send(){
      var txt,msg;
      txt = $("msg");
      msg = txt.value;
      if(!msg){ alert("Message can not be empty"); return; }
      txt.value="";
      txt.focus();
      try{ socket.send(msg); log('Sent: '+msg); } catch(ex){ log(ex); }
    }

    Sorry if this is a repost browser froze submitting.

    thinking I'm not using the quotes right? I know JavaScript is weird about quotes. I tried /" but was getting syntax errors.

    I can post the whole script if needed.

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787

  3. #3
    Join Date
    Nov 2012
    Posts
    3
    thanks for the reply.

    I think im seeing whats going on here. {"op":"addr_sub", "addr":"address"} is a JSON object. correct?

    To send it I need to turn it into a string?

    I tried just changing try{ JSON.stringify(msg); log('Sent: '+msg); } catch(ex){ log(ex); }
    }

    And im getting
    Sent: [object Object]
    I'm reading up on JSON objects.

    http://www.w3schools.com/json/json_intro.asp

    in the example it uses
    var JSONObject=

    So I edited my code to

    Code:
    function send(){
      var txt,msg,address,JSONObject;
      var JSONObject = {
      "op":"addr_sub",
      "addr":"address"};  
      txt = $("msg");
      address = txt.value
      msg = JSONObject  
      if(!msg){ alert("Message can not be empty"); return; }
      txt.value="";
      txt.focus();
      try{ socket.send(JSON.stringify(msg)); log('Sent: '+msg); } catch(ex){ log(ex); }
    //document.write(msg);
    }
    Im still getting the silly [object Object] I did a document.write and it shows [object Object] I have no idea where that is coming from

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    there's a lot of bugs there, so i just rewrote it

    i think you meant to use id selectors, since there is no <msg> tag...


    Code:
    function send(){
      var txt=$("msg"), msg;
    
      msg = {
         "op" :  "addr_sub",
        "addr":  $("#msg").val() || "no value set..."
      }; 
     
    
      txt.val("");
      txt.focus();
    
      socket.send(JSON.stringify(msg)); 
      document.write(  "<pre>" + JSON.stringify(msg, null, "\t")  );
    }
    hopefully that get's you closer...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles