I made a Character
class shared by the client and server in the same js file.
The server instanciates these characters and stocks them in a characterList
object. I send it to the client with socket.emit( 'updated_characters', characterList )
, but the client gets back an object of Object
, instead of an object of Character
, so I can't use my Character's methods on them.
How can I get around that ?
You can't send custom object types directly using socket.io. socket.io uses JSON as the transport format for objects and JSON does not have the ability to record or rebuild custom object types.
So, what you need to do is to create a serialized format for your objects (which will also likely be JSON) and then when you read the data on the other end, you will construct a custom object in your own code and then pass it this data in the constructor from which it will initialize itself.
The canonical ways to do this is to create a serialize method for your custom object that creates a new plain object that contains all the relevant info that needs to be sent over the wire and does not contain references to other objects (since that can't be serialized). Keep in mind that objects often have housekeeping information that does not necessarily need to be sent to the other end. Then, create an initialize method for your custom object that can take this serialized data and properly initialize your object that you can use on the other end.
Here's a simple example:
function MyObject(data) {
if (data) {
this.init(data);
} else {
this.count = 0;
this.greeting = "hello"
}
}
MyObject.prototype = {
init: function(data) {
this.greeting = data.greeting;
this.count = data.count;
},
getData: function() {
return {greeting: this.greeting, count: this.count};
},
talk: function() {
say(this.greeting);
}
};
Then, on the sending end of things, if you have an instance of your object in item
, you would send the data with:
socket.emit('updated_characters', item.getData());
And, on the receiving side of things, you would have this:
socket.on('updated_characters', function(data) {
var item = new MyObject(data);
// do something with the item here
});
I have two divs <div class="tab-pane active" id="home"> <div class="tab-pane" id="profile"> Each of this div has forms in it which has a hidden field <input type="hidden" name="...
I have two divs <div class="tab-pane active" id="home"> <div class="tab-pane" id="profile"> Each of this div has forms in it which has a hidden field <input type="hidden" name="...
Currently I am using React on Rails application (using react-rails library) and I wish to use some third party React components. How to use third party component without adding dependency to ...
Currently I am using React on Rails application (using react-rails library) and I wish to use some third party React components. How to use third party component without adding dependency to ...
Update: A Solution below! I'm fairly new to website development but I've been tasked with developing e2e (end-to-end) tests for a developing website that uses AngularJS. As a result I've been looking ...
Update: A Solution below! I'm fairly new to website development but I've been tasked with developing e2e (end-to-end) tests for a developing website that uses AngularJS. As a result I've been looking ...
Apologies in advanced, but I'm a complete noob at Polymer. So far I've done an HTML file (treated as external dialog box), with some stuff in it. First off, the user clicks a button from the main ...
Apologies in advanced, but I'm a complete noob at Polymer. So far I've done an HTML file (treated as external dialog box), with some stuff in it. First off, the user clicks a button from the main ...