tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/2394-sockets-server-handling-networking-in-multiple-servicesRobotlegs: Discussion 2013-08-17T10:52:46Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/276371722013-07-07T17:42:05Z2013-07-07T17:42:05ZSockets: Server Handling Networking in multiple services<div><p>Hi. The "stack" approach sounds good. It sounds very similar to
what I do - wrap a low level REST service instance with higher
level application specific services.</p>
<blockquote>
<p>first question: How would you handle the connection objects ?
Storing them in the ConnectionsHandler service or in a model and
let the service inject it?</p>
</blockquote>
<p>Keep them in a model, and inject that model into your
service.</p>
<blockquote>
<p>Second question: where i store the authenticated clients
(userObject)?? Model? service?</p>
</blockquote>
<p>Perhaps in AuthenticatedClientsModel?</p>
<blockquote>
<p>Do you think that's a good architecture?</p>
</blockquote>
<p>It <em>could</em> be a good architecture, but no one can say
without using it for a while. Try it out, see how it feels. If
there are any bits that feel "fragile", "repetitive" or otherwise
"painful" then change those bits.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/276371722013-07-08T09:34:02Z2013-07-08T09:34:02ZSockets: Server Handling Networking in multiple services<div><p>Thank you Shaun, i'm having always the same problem across
several project and until now i've not found a solution:</p>
<p>To indicate the messages types, i'm using a byte variable, i
don't know how to convert that byte to the corresponding object and
dispatch the corresponding event in a correct and fast way.<br>
I know there is the old switch statement but i feel like that it's
not the correct way to handle this, maybe i'm wrong and all the
online games and network applications use the switch but i don't
think so.<br>
I've search all the web for information about network message
handling protocol but i've not found anything useful</p></div>fbregisttag:robotlegs.tenderapp.com,2009-10-18:Comment/276371722013-07-21T15:18:57Z2013-07-21T15:18:57ZSockets: Server Handling Networking in multiple services<div><p>By the way, there is a very simple refactor for the Switch
anti-pattern where you introduce a "config" object.</p>
<p>Instead of doing this (psuedo-code):</p>
<pre>
<code>switch(message)
case "hello":
x = 100;
break;
case "world"
x = 200;
break;</code>
</pre>
<p>You can do this:</p>
<pre>
<code>private const config:Object = {
hello: 100,
world: 200 };
// .. and then later you simply use that config as a look up
x = config[message];</code>
</pre>
<p>You can almost always do this in places where you have a switch.
If you need to call methods you just create a config object that
has function references instead of values:</p>
<pre>
<code>private const config:Object = {
hello: method1,
world: method2 };
// .. and then later you simply use that config as a look up, and call
config[message]();</code>
</pre>
<p>What happens in terms of reading the code is that, when you name
things well (don't actually call it "config"!), the code becomes
easier to parse mentally. You often don't even need to look at the
config object to see what the calling code is trying to do. With a
switch you basically have to read through all the switch cases to
see what the "intent" is.</p>
<p>What happens in terms of performance is that you end up with
replacing a long series of "checks" with a single lookup.</p></div>Shaun Smith