Get a Jumpstart with our Sample Apps
Results 1 to 8 of 8

Thread: Difficulty replicating WebSockets demo...

  1. #1
    Member
    Real Name
    Mark Bunds
    Join Date
    Aug 2015
    Location
    Cypress, TX
    Posts
    68

    Post Difficulty replicating WebSockets demo...

    I followed the web sockets video tutorial to the letter (as far as I can tell) but when I send a message, the receiving browser displays an alert saying: "Received a message that was not in correct format. Messages must be JSON strings".

    The local instance of the list in each browser updates as expected with its own message that was attempted to broadcast.

    As the video shows, I entered the following into the button click event, with an additional alert "alert(JSON.stringify(obj));" to see what is being sent:

    Code:
    var _name = {dialog.object}.getValue('name'); 
    var _message = {dialog.object}.getValue('messageText');
    
    if (_name == '') {
    	alert('Please Enter Your User Name.');
    	return false;
    }
    
    if (_message == '') {
    	alert('Please Enter a Message.');
    	return false;
    }
    
    var _d = {name: _name, message: _message};
    var _list = {dialog.object}.getControl('listMessages');
    _list.appendRows([_d]);
    
    //create message as a JSON string
    var obj = {type: "messageBoard", name: _name, messageText: _message};
    
    //reset the message text to blank
    {dialog.object}.setValue('messageText','');
    
    alert(JSON.stringify(obj));
    
    return JSON.stringify(obj);
    And the following code is in the client-side "webSocketOnMessage" event trigger, again with the additional alert to show what was received::

    Code:
    if(e.message != '') {
    alert(JSON.stringify(e.message));
    	var p = JSON.parse(e.message);
    	if(p.type = 'messageboard') {
    	var _d = [{name: p.name, message: p.messageText}];
    	var lObj = {dialog.object}.getControl('listMessages');
    	lObj.appendRows(_d);
    	
    	}
    	
    }
    After launching a live preview, and a browser preview (launching each using "Full Preview"), and then attempting to send a message from one browser, or the other, using "Test Message 1" as the message, and "User 1" as the user name, the additional alerts show the following:

    The extra alert in the SENDING browser shows: {"type":"messageBoard","name":"User 1","messageText":"Test Message 1"}

    The extra alert in the RECEIVING browser shows:{"type":"messageBoard","name":"User 1","messageText":"Test Message 1","_sequence":9,"_id":"55900C9B-43AD-4C96-9F8D-E8C2E2C83020"}, after which the RECEIVING browser presents its own alert showing: "Received a message that was not in correct format. Messages must be JSON strings"

    It looks like the message is being broadcast, and received by the listening browser instances, but as soon as the client-side event "webSocketOnMessage" fires and tries to parse the message received, it complains about JSON formatting at "var p = JSON.parse(e.message)" because it can't parse the string as JSON string data, even though I used "JSON.stringify(obj)" in the return statement of the button click event.

    This seems like a simple, straight-forward example. What am I overlooking?

    Thanks in advance!

  2. #2
    Member
    Real Name
    Mark Bunds
    Join Date
    Aug 2015
    Location
    Cypress, TX
    Posts
    68

    Default Re: Difficulty replicating WebSockets demo...

    I got it to work.

    I don't know why, but to make the JSON.parse command work as the demo shows, I had to change it from "var p = JSON.parse(e.message);" to "var p = JSON.parse(JSON.stringify(e.message));", even though the button click event has the command "return JSON.stringify(obj);" in its JavaScript event handler return statement.

  3. #3
    "Certified" Alphaholic peteconway's Avatar
    Real Name
    Peter Conway
    Join Date
    Oct 2005
    Location
    Melbourne, Australia
    Posts
    2,364

    Default Re: Difficulty replicating WebSockets demo...

    Man - thanks for posting this - it was driving me nuts. - Good solve.
    Insanity: doing the same thing over and over again and expecting different results.
    Albert Einstein, (attributed)
    US (German-born) physicist (1879 - 1955)

  4. #4
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,249

    Default Re: Difficulty replicating WebSockets demo...

    "var p = JSON.parse(JSON.stringify(e.message));"
    pete,
    while you have solved the problem, i am just curious.
    in the above statement it seems that you are converting the e.message ( appears to be an object) to a string using JSON.stringify() then back to an object with JSON.parse the result.
    can you not just use the e.message itself? like var p = e.message;
    i cannot test since i do not have version 12, i am just curious.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  5. #5
    "Certified" Alphaholic peteconway's Avatar
    Real Name
    Peter Conway
    Join Date
    Oct 2005
    Location
    Melbourne, Australia
    Posts
    2,364

    Default Re: Difficulty replicating WebSockets demo...

    Well, what it did was show me what was going on and that the issue was running before the message had it's full value from what I could see, anyhow all working now - thanks.
    Insanity: doing the same thing over and over again and expecting different results.
    Albert Einstein, (attributed)
    US (German-born) physicist (1879 - 1955)

  6. #6
    Member
    Real Name
    Michael Cornford
    Join Date
    Oct 2011
    Location
    East Sussex, UK
    Posts
    839

    Default Re: Difficulty replicating WebSockets demo...

    This post was a life saver.

    can you not just use the e.message itself? like var p = e.message;
    Gandhi this works!

  7. #7
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,249

    Default Re: Difficulty replicating WebSockets demo...

    michael, it made me more curious so i watched the video.
    the first post from mark quotes the code from the video verbatim
    //create message as a JSON string
    var obj = {type: "messageBoard", name: _name, messageText: _message};
    the red coloring is mine.
    this is an error, selwyn created an object and gave a wrong information stating that he created a string. it should have said create a JSON object then all will be understandable. if you notice following that line he is sending an alert
    alert(JSON.stringify(obj)); // you cannot stringify a string .
    this has led everyone on a primrose path.
    then you will understand why the code did not need stringify the e.message, just parse it to an object, p, to get the elements.
    if this is corrected then no one will need to stringify a string that is already there. the concept went wrong from then on.
    this is how i see it.
    you can test it in a simple ux/dialog and you will see it is really an object that is created not a string.
    put a simple button
    and in the onclick event declare
    var obj = {name: "gandhi", age:66};
    alert(obj);
    alert(obj.name);
    alert(obj.age);
    you will see it is an obj.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  8. #8
    Member
    Real Name
    Michael Cornford
    Join Date
    Oct 2011
    Location
    East Sussex, UK
    Posts
    839

    Default Re: Difficulty replicating WebSockets demo...

    Thanks,

    I agree and understand. In following new code we are often too focused on maintaining the syntax than following the logic.

    Anyone hitting this problem should search the message board so your explanation will put them on the correct path.

Similar Threads

  1. Replicating Reports
    By nuggetsoftware in forum Alpha Five Version 11 - Desktop Applications
    Replies: 1
    Last Post: 02-06-2012, 01:16 PM
  2. Replicating records
    By 180jase in forum Alpha Five Version 6
    Replies: 3
    Last Post: 10-20-2006, 09:13 AM
  3. Look up difficulty
    By BDennis in forum Alpha Five Version 4
    Replies: 4
    Last Post: 12-16-2002, 08:39 AM
  4. Problem with A5 Menu bar replicating
    By Richard Hale in forum Alpha Five Version 4
    Replies: 1
    Last Post: 04-09-2002, 07:08 PM
  5. query difficulty
    By martinwcole in forum Alpha Five Version 4
    Replies: 2
    Last Post: 08-07-2001, 08:15 PM

Posting Permissions

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