Appropriate way to handle data received from the server

Asked

Viewed 29 times

0

Send something, a username for example writer.WriteLine(Username.text);

And after processing the data on the server, I return something like:

stwSend.WriteLine("a2|Cadastro efetuado com sucesso!");

And then on the client side, I wait with a parole

if(data.Substring(0, 2) == "a2")
 {
     var result = data.Substring(3);
 }

I’ve been doing this a lot these past few days, so I wonder if there’s a proper way to do this.

1 answer

1


If you’re building a service Hostwho will exchange messages with your clients, the ideal would be to use as a basis the standards already established for this type of operation, such as XML or JSON, you might think about leveraging the resources of the framework and providing that interaction as a SOAP, WCF or a Web API REST

Not that it’s impossible to implement the way you put it, but instead substring() I would make a split() for | to split arguments. Making this a rule of your contract model.

{string} | {string message}

var mensagem = data.split('|'); 
if(mensagem[0] == "a2") //E demais validações
{
   var result = mensagem[1];
}

As it stands, you’ll soon face problems in case you have to answer a code "a10" or if you need to increase the set of information sent or received as parameters other than code and message. This practice will still bring you more problems if you intend to distribute your service to third parties to make their own implementations to consume it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.