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:
And the following code is in the client-side "webSocketOnMessage" event trigger, again with the additional alert to show what was received::
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!
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);
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); } }
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!
Comment