www.webdeveloper.com
Results 1 to 4 of 4

Thread: socket.send Troubleshooting Javascript websocket send

  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
    socket.send(JSON.stringify(msg));
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

  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...
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.12405 seconds
  • Memory Usage 2,874KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (5)bbcode_code
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates