Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

9 years ago by stcredzero

Howdy,

I'm writing a multiplayer game on the desktop. I've written a websockets HTML5 game before. (currently at http://emergencevector.com) However, that game had a roll-your-own game loop. With ImpactJS, I'm a little confused as to where I should put my websockets event functions. I currently have them in the game's init. I find this problematic because of the weird semantics around the this keyword in Javascript. I don't want my websocket object accidentally stuck into another object that invoked the init() on the game. I've also thought about establishing my own global websocket management object, and having each entity register for callbacks by UUID. Any thoughts? Does the way ImpactJS put together allow me to stick properties on 'this' at init() time and not worry about values being stuck onto the caller?

9 years ago by Joncom

In other words, where is the best place to put all the socket server callback functions?

Good question. I'm not entirely sure, and I'm doing the same as you so far (setting them up in the game init function). There must be a cleaner/better way, but so far it's been working fairly well.

As for the "this" keyword pointing to things you don't want it to, isn't that what bind and call are for?

9 years ago by drhayes

I've also thought about establishing my own global websocket management object, and having each entity register for callbacks by UUID.


Sounds good to me. Your onmessage can do the dispatch to the entities or set state or whatever... and, yeah, like Joncom said, just bind the function to the instance you care about or make it a well-known function, e.g. every Entity has a function named onWsMessage so your WS dispatch goes entities[message.entityId].onWsMessage(data); or something. Or callbacks[message.entityId]() with every callback being a entity.onMessage.bind(entity) or something.

I'm not at my writing best but your solution sounds good to me.
Page 1 of 1
« first « previous next › last »