Passing Complex object via web sockets












0














Using .NetCore...



I have a C# server side code.



It creates an object list like:



[Serializable]
public class MyObject
{
public string test { get; set;}
}

var manyOfTheseObjects = new List<MyObject>();

manyOfTheseObjects ~ add a few records


I now convert to a ByteArray ~



var binFormatter = new BinaryFormatter();
var mStream = new MemoryStream();
binFormatter.Serialize(mStream, manyOfTheseObjects);
socket.Send(mStream.ToArray());


On the JavaScript side I am listening on the web socket:



ws.onopen = function (data) {
try {
console.log("onopen");
console.log(JSON.parse(data).result);
$("#divConnectionStatus").html("Client connected");
resume= 1;}
catch (err) {
console.log(err);
$("#divConnectionStatus").html("onopen: " + err);
}


};



I get the error:



SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at WebSocket.Connect.ws.onopen (LiveFeed.js:182)


I am obviously doing this completely wrong...



NB



Changed this:



var binFormatter = new BinaryFormatter();
var mStream = new MemoryStream();
binFormatter.Serialize(mStream, manyOfTheseObjects);
socket.Send(mStream.ToArray());


to this:



socket.Send( JsonConvert.SerializeObject(manyOfTheseObjects ) );









share|improve this question




















  • 1




    A byte array is not necessarily valid JSON. You should probably use JsonConvert.SerializeObject() if you want JSON...
    – Heretic Monkey
    Nov 12 '18 at 19:11










  • just changed it to that. same error. Will update my question with that code
    – Andrew Simpson
    Nov 12 '18 at 19:26










  • Add what you get when you do console.log(data) within your onopen handler.
    – Heretic Monkey
    Nov 12 '18 at 19:28












  • BinaryFormatter is a .NET-specific binary serializer (and frankly: one I would avoid) - it isn't JSON. If you want: use a JSON serializer.
    – Marc Gravell
    Nov 12 '18 at 19:28










  • with the edit; what does the received payload turn out to be? As @HereticMonkey asks: what is console.log(data) ?
    – Marc Gravell
    Nov 12 '18 at 19:29
















0














Using .NetCore...



I have a C# server side code.



It creates an object list like:



[Serializable]
public class MyObject
{
public string test { get; set;}
}

var manyOfTheseObjects = new List<MyObject>();

manyOfTheseObjects ~ add a few records


I now convert to a ByteArray ~



var binFormatter = new BinaryFormatter();
var mStream = new MemoryStream();
binFormatter.Serialize(mStream, manyOfTheseObjects);
socket.Send(mStream.ToArray());


On the JavaScript side I am listening on the web socket:



ws.onopen = function (data) {
try {
console.log("onopen");
console.log(JSON.parse(data).result);
$("#divConnectionStatus").html("Client connected");
resume= 1;}
catch (err) {
console.log(err);
$("#divConnectionStatus").html("onopen: " + err);
}


};



I get the error:



SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at WebSocket.Connect.ws.onopen (LiveFeed.js:182)


I am obviously doing this completely wrong...



NB



Changed this:



var binFormatter = new BinaryFormatter();
var mStream = new MemoryStream();
binFormatter.Serialize(mStream, manyOfTheseObjects);
socket.Send(mStream.ToArray());


to this:



socket.Send( JsonConvert.SerializeObject(manyOfTheseObjects ) );









share|improve this question




















  • 1




    A byte array is not necessarily valid JSON. You should probably use JsonConvert.SerializeObject() if you want JSON...
    – Heretic Monkey
    Nov 12 '18 at 19:11










  • just changed it to that. same error. Will update my question with that code
    – Andrew Simpson
    Nov 12 '18 at 19:26










  • Add what you get when you do console.log(data) within your onopen handler.
    – Heretic Monkey
    Nov 12 '18 at 19:28












  • BinaryFormatter is a .NET-specific binary serializer (and frankly: one I would avoid) - it isn't JSON. If you want: use a JSON serializer.
    – Marc Gravell
    Nov 12 '18 at 19:28










  • with the edit; what does the received payload turn out to be? As @HereticMonkey asks: what is console.log(data) ?
    – Marc Gravell
    Nov 12 '18 at 19:29














0












0








0







Using .NetCore...



I have a C# server side code.



It creates an object list like:



[Serializable]
public class MyObject
{
public string test { get; set;}
}

var manyOfTheseObjects = new List<MyObject>();

manyOfTheseObjects ~ add a few records


I now convert to a ByteArray ~



var binFormatter = new BinaryFormatter();
var mStream = new MemoryStream();
binFormatter.Serialize(mStream, manyOfTheseObjects);
socket.Send(mStream.ToArray());


On the JavaScript side I am listening on the web socket:



ws.onopen = function (data) {
try {
console.log("onopen");
console.log(JSON.parse(data).result);
$("#divConnectionStatus").html("Client connected");
resume= 1;}
catch (err) {
console.log(err);
$("#divConnectionStatus").html("onopen: " + err);
}


};



I get the error:



SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at WebSocket.Connect.ws.onopen (LiveFeed.js:182)


I am obviously doing this completely wrong...



NB



Changed this:



var binFormatter = new BinaryFormatter();
var mStream = new MemoryStream();
binFormatter.Serialize(mStream, manyOfTheseObjects);
socket.Send(mStream.ToArray());


to this:



socket.Send( JsonConvert.SerializeObject(manyOfTheseObjects ) );









share|improve this question















Using .NetCore...



I have a C# server side code.



It creates an object list like:



[Serializable]
public class MyObject
{
public string test { get; set;}
}

var manyOfTheseObjects = new List<MyObject>();

manyOfTheseObjects ~ add a few records


I now convert to a ByteArray ~



var binFormatter = new BinaryFormatter();
var mStream = new MemoryStream();
binFormatter.Serialize(mStream, manyOfTheseObjects);
socket.Send(mStream.ToArray());


On the JavaScript side I am listening on the web socket:



ws.onopen = function (data) {
try {
console.log("onopen");
console.log(JSON.parse(data).result);
$("#divConnectionStatus").html("Client connected");
resume= 1;}
catch (err) {
console.log(err);
$("#divConnectionStatus").html("onopen: " + err);
}


};



I get the error:



SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at WebSocket.Connect.ws.onopen (LiveFeed.js:182)


I am obviously doing this completely wrong...



NB



Changed this:



var binFormatter = new BinaryFormatter();
var mStream = new MemoryStream();
binFormatter.Serialize(mStream, manyOfTheseObjects);
socket.Send(mStream.ToArray());


to this:



socket.Send( JsonConvert.SerializeObject(manyOfTheseObjects ) );






javascript c# websocket






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 19:27







Andrew Simpson

















asked Nov 12 '18 at 19:09









Andrew SimpsonAndrew Simpson

2,783737101




2,783737101








  • 1




    A byte array is not necessarily valid JSON. You should probably use JsonConvert.SerializeObject() if you want JSON...
    – Heretic Monkey
    Nov 12 '18 at 19:11










  • just changed it to that. same error. Will update my question with that code
    – Andrew Simpson
    Nov 12 '18 at 19:26










  • Add what you get when you do console.log(data) within your onopen handler.
    – Heretic Monkey
    Nov 12 '18 at 19:28












  • BinaryFormatter is a .NET-specific binary serializer (and frankly: one I would avoid) - it isn't JSON. If you want: use a JSON serializer.
    – Marc Gravell
    Nov 12 '18 at 19:28










  • with the edit; what does the received payload turn out to be? As @HereticMonkey asks: what is console.log(data) ?
    – Marc Gravell
    Nov 12 '18 at 19:29














  • 1




    A byte array is not necessarily valid JSON. You should probably use JsonConvert.SerializeObject() if you want JSON...
    – Heretic Monkey
    Nov 12 '18 at 19:11










  • just changed it to that. same error. Will update my question with that code
    – Andrew Simpson
    Nov 12 '18 at 19:26










  • Add what you get when you do console.log(data) within your onopen handler.
    – Heretic Monkey
    Nov 12 '18 at 19:28












  • BinaryFormatter is a .NET-specific binary serializer (and frankly: one I would avoid) - it isn't JSON. If you want: use a JSON serializer.
    – Marc Gravell
    Nov 12 '18 at 19:28










  • with the edit; what does the received payload turn out to be? As @HereticMonkey asks: what is console.log(data) ?
    – Marc Gravell
    Nov 12 '18 at 19:29








1




1




A byte array is not necessarily valid JSON. You should probably use JsonConvert.SerializeObject() if you want JSON...
– Heretic Monkey
Nov 12 '18 at 19:11




A byte array is not necessarily valid JSON. You should probably use JsonConvert.SerializeObject() if you want JSON...
– Heretic Monkey
Nov 12 '18 at 19:11












just changed it to that. same error. Will update my question with that code
– Andrew Simpson
Nov 12 '18 at 19:26




just changed it to that. same error. Will update my question with that code
– Andrew Simpson
Nov 12 '18 at 19:26












Add what you get when you do console.log(data) within your onopen handler.
– Heretic Monkey
Nov 12 '18 at 19:28






Add what you get when you do console.log(data) within your onopen handler.
– Heretic Monkey
Nov 12 '18 at 19:28














BinaryFormatter is a .NET-specific binary serializer (and frankly: one I would avoid) - it isn't JSON. If you want: use a JSON serializer.
– Marc Gravell
Nov 12 '18 at 19:28




BinaryFormatter is a .NET-specific binary serializer (and frankly: one I would avoid) - it isn't JSON. If you want: use a JSON serializer.
– Marc Gravell
Nov 12 '18 at 19:28












with the edit; what does the received payload turn out to be? As @HereticMonkey asks: what is console.log(data) ?
– Marc Gravell
Nov 12 '18 at 19:29




with the edit; what does the received payload turn out to be? As @HereticMonkey asks: what is console.log(data) ?
– Marc Gravell
Nov 12 '18 at 19:29












1 Answer
1






active

oldest

votes


















1














Now that you've switch to a JSON serializer: you want onmessage, not onopen:



ws.onmessage = function (evt) {
try {
console.log("onmessage");
console.log(JSON.parse(evt.data).result);
$("#divConnectionStatus").html("Message received");
resume= 1;}
catch (err) {
console.log(err);
$("#divConnectionStatus").html("onmessage: " + err);
}





share|improve this answer























  • Thanks for ur answer. I will update my qquestion with the console.log(data); in a mo
    – Andrew Simpson
    Nov 12 '18 at 19:33










  • When I initially request a connection credentials ae sent back from server to client. It is quite acceptable for me to catch the message there?
    – Andrew Simpson
    Nov 12 '18 at 19:34










  • @AndrewSimpson no, because that isn't the event that gets raised for messages; onmessage is
    – Marc Gravell
    Nov 12 '18 at 19:34










  • I shall recheck. :)
    – Andrew Simpson
    Nov 12 '18 at 19:35










  • thanks v much, u r a smart cool dude..
    – Andrew Simpson
    Nov 12 '18 at 19:39











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268591%2fpassing-complex-object-via-web-sockets%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Now that you've switch to a JSON serializer: you want onmessage, not onopen:



ws.onmessage = function (evt) {
try {
console.log("onmessage");
console.log(JSON.parse(evt.data).result);
$("#divConnectionStatus").html("Message received");
resume= 1;}
catch (err) {
console.log(err);
$("#divConnectionStatus").html("onmessage: " + err);
}





share|improve this answer























  • Thanks for ur answer. I will update my qquestion with the console.log(data); in a mo
    – Andrew Simpson
    Nov 12 '18 at 19:33










  • When I initially request a connection credentials ae sent back from server to client. It is quite acceptable for me to catch the message there?
    – Andrew Simpson
    Nov 12 '18 at 19:34










  • @AndrewSimpson no, because that isn't the event that gets raised for messages; onmessage is
    – Marc Gravell
    Nov 12 '18 at 19:34










  • I shall recheck. :)
    – Andrew Simpson
    Nov 12 '18 at 19:35










  • thanks v much, u r a smart cool dude..
    – Andrew Simpson
    Nov 12 '18 at 19:39
















1














Now that you've switch to a JSON serializer: you want onmessage, not onopen:



ws.onmessage = function (evt) {
try {
console.log("onmessage");
console.log(JSON.parse(evt.data).result);
$("#divConnectionStatus").html("Message received");
resume= 1;}
catch (err) {
console.log(err);
$("#divConnectionStatus").html("onmessage: " + err);
}





share|improve this answer























  • Thanks for ur answer. I will update my qquestion with the console.log(data); in a mo
    – Andrew Simpson
    Nov 12 '18 at 19:33










  • When I initially request a connection credentials ae sent back from server to client. It is quite acceptable for me to catch the message there?
    – Andrew Simpson
    Nov 12 '18 at 19:34










  • @AndrewSimpson no, because that isn't the event that gets raised for messages; onmessage is
    – Marc Gravell
    Nov 12 '18 at 19:34










  • I shall recheck. :)
    – Andrew Simpson
    Nov 12 '18 at 19:35










  • thanks v much, u r a smart cool dude..
    – Andrew Simpson
    Nov 12 '18 at 19:39














1












1








1






Now that you've switch to a JSON serializer: you want onmessage, not onopen:



ws.onmessage = function (evt) {
try {
console.log("onmessage");
console.log(JSON.parse(evt.data).result);
$("#divConnectionStatus").html("Message received");
resume= 1;}
catch (err) {
console.log(err);
$("#divConnectionStatus").html("onmessage: " + err);
}





share|improve this answer














Now that you've switch to a JSON serializer: you want onmessage, not onopen:



ws.onmessage = function (evt) {
try {
console.log("onmessage");
console.log(JSON.parse(evt.data).result);
$("#divConnectionStatus").html("Message received");
resume= 1;}
catch (err) {
console.log(err);
$("#divConnectionStatus").html("onmessage: " + err);
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 19:37

























answered Nov 12 '18 at 19:32









Marc GravellMarc Gravell

778k19221262540




778k19221262540












  • Thanks for ur answer. I will update my qquestion with the console.log(data); in a mo
    – Andrew Simpson
    Nov 12 '18 at 19:33










  • When I initially request a connection credentials ae sent back from server to client. It is quite acceptable for me to catch the message there?
    – Andrew Simpson
    Nov 12 '18 at 19:34










  • @AndrewSimpson no, because that isn't the event that gets raised for messages; onmessage is
    – Marc Gravell
    Nov 12 '18 at 19:34










  • I shall recheck. :)
    – Andrew Simpson
    Nov 12 '18 at 19:35










  • thanks v much, u r a smart cool dude..
    – Andrew Simpson
    Nov 12 '18 at 19:39


















  • Thanks for ur answer. I will update my qquestion with the console.log(data); in a mo
    – Andrew Simpson
    Nov 12 '18 at 19:33










  • When I initially request a connection credentials ae sent back from server to client. It is quite acceptable for me to catch the message there?
    – Andrew Simpson
    Nov 12 '18 at 19:34










  • @AndrewSimpson no, because that isn't the event that gets raised for messages; onmessage is
    – Marc Gravell
    Nov 12 '18 at 19:34










  • I shall recheck. :)
    – Andrew Simpson
    Nov 12 '18 at 19:35










  • thanks v much, u r a smart cool dude..
    – Andrew Simpson
    Nov 12 '18 at 19:39
















Thanks for ur answer. I will update my qquestion with the console.log(data); in a mo
– Andrew Simpson
Nov 12 '18 at 19:33




Thanks for ur answer. I will update my qquestion with the console.log(data); in a mo
– Andrew Simpson
Nov 12 '18 at 19:33












When I initially request a connection credentials ae sent back from server to client. It is quite acceptable for me to catch the message there?
– Andrew Simpson
Nov 12 '18 at 19:34




When I initially request a connection credentials ae sent back from server to client. It is quite acceptable for me to catch the message there?
– Andrew Simpson
Nov 12 '18 at 19:34












@AndrewSimpson no, because that isn't the event that gets raised for messages; onmessage is
– Marc Gravell
Nov 12 '18 at 19:34




@AndrewSimpson no, because that isn't the event that gets raised for messages; onmessage is
– Marc Gravell
Nov 12 '18 at 19:34












I shall recheck. :)
– Andrew Simpson
Nov 12 '18 at 19:35




I shall recheck. :)
– Andrew Simpson
Nov 12 '18 at 19:35












thanks v much, u r a smart cool dude..
– Andrew Simpson
Nov 12 '18 at 19:39




thanks v much, u r a smart cool dude..
– Andrew Simpson
Nov 12 '18 at 19:39


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268591%2fpassing-complex-object-via-web-sockets%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values