Flex: Um chat simples em menos de 20 linhas.

Uma coisas mais excepcionais que o design do framework do Adobe Flex traz, é a facilidade de trabalhar com os serviços de mensageria. Toda a complexidade que existe nas soluções de mensageria como a do JMS do Java, no Flex, basicamente foi resumido ao <mx:Producer/> e <mx:Consumer/> (ver a documentação).

Assim como os outros meios de comunicação do Flex ( HttpService, WebService, RemoteObject ) o Consumer e Producer precisam de uma funcionalidade no back-end, em específico o DataService que está usando. Como por exemplo, no Java o DataService mais comum que contém essa funcionalidade é o Live Cycle Data Services e o BlazeDS, este OpenSource. No PHP, tem o WebOrb for PHP.

Como o título deste tópico informa, para provar que é muito simples trabalhar com mensageria no Flex, vamos criar um chat com nem 20 linhas (um total de linhas programadas e não de configuração) no Flex. Para tal façanha, primeiro escolha qual linguagem e DataService que irá utilizar no back-end. Eu estarei utilizando Java + BlazeDS.

Partindo do princípio que o BlazeDS já está integrado ao Java e com as configurações básicas:

  1. O primeiro passo é criar um canal de comunicação para trafegar as mensagens. Para isso, no arquivo services-config.xml do BlazeDS, adicione o seguinte canal:

    [xml]

    [/xml]

    Esse é um canal de Streaming, e só irá funcionar se for executado em Http 1.1. Outra forma de utilizar os serviços de menssageria, é criar um canal AMF normal (AMFChannel) e ativar o polling, como no exemplo abaixo:

    [xml]

    true

    [/xml]

  2. Criado o canal, vamos criar o destination na qual irá utilizar o canal. Para isso, crie se não existir um arquivo no mesmo diretório do services-config.xml com o nome de messaging-config.xml, nele coloque o seguinte conteúdo:
  3. [xml]

    [/xml]

    Com o messaging-config.xml criado, é preciso informar ao services-config.xml que existem destinations de mensageria. Para isso adicione as linhas abaixo no services-config.xml:

    [xml]

    [/xml]

  4. Adicionado o destination (chatMessage), a configuração já está completa. Vamos agora criar a Interface do Chat junto com o Producer e Consumer (menos de 20 linhas):

    [xml]

    [/xml]

Pronto! agora basta startar seu Container Web (no meu caso o TomCat) e abrir 2 browsers distintos e enviar as menssagens.
Atenção com o Firefox, pois ele armazena a mesma session para abas diferentes, e por default, o BlazeDS suporta uma conexão por cliente.

Para todo e qualquer erro, verifique sempre o console do Tomcat, ele sempre traz informações úteis para identificar os erros.

[Download do Fonte]

ChatApp

29 thoughts on “Flex: Um chat simples em menos de 20 linhas.

  1. Pingback: Chat MSN » Bruno bg + ADOBE FLEX

  2. tekne kiralama

    You have read very well this year. I hope you have had fun and learned a lot. One of my favorite books was “The Very Clumsy Click Beetle”. I like the book we are making. I hope you have a good year.

    Like

    Reply
  3. mrscabies302

    You must pay off ones bills in time since it can have a negative impact on your credit rating history as well as standing. Additionally , you will be capable to steer clear of acquiring charged on account of definitely not compensating on time. Try out wanting to know your plastic card business to take out the actual past due ask for in case you have forgotten to cover the item in time with regard to once.

    Like

    Reply
  4. pandora charms

    Thank you! You often write very interesting articles. You improved my mood. I was able to get approved for two credit cards that offered zero percent interest on balance transfers for two years one with a ten thousand dollar credit limit, the other with a five thousand dollar limit.

    Like

    Reply
  5. dui laws in california

    I’m happy I found this weblog, I couldnt find any info on this subject matter prior to. I also run a site and if you want to ever serious in a little bit of guest writing for me if achievable feel free to let me know, i’m always look for people to examine out my site. Please stop by and leave a comment sometime!

    Like

    Reply
  6. Free NGP

    Just a fast hello and also to thank you for discussing your ideas on this web page. I wound up in your blog right after researching physical fitness connected issues on Yahoo… guess I lost track of what I had been performing! Anyway I’ll be back once once more within the long run to examine out your blogposts down the road. Thanks!

    Like

    Reply
  7. rob gell

    This was a really incredibly excellent submit. In theory I’d like to write like this also – getting time and actual effort to make a great piece of writing… but what can I say… I procrastinate alot and by no means appear to obtain something done.

    Like

    Reply
  8. johnny smoes

    Fantastic job right here. I genuinely enjoyed what you had to say. Keep going because you definitely bring a new voice to this topic. Not many people would say what youve said and still make it interesting. Nicely, at least Im interested. Cant wait to see a lot more of this from you.

    Like

    Reply
  9. increase your sales

    I’m happy I found this weblog, I couldnt learn any information on this topic matter prior to. I also run a site and if you want to ever serious in a little bit of guest writing for me if achievable really feel free to let me know, i’m always appear for people to examine out my site. Please stop by and leave a comment sometime!

    Like

    Reply
  10. Criminal Attorney Riverside

    Thanks for making the sincere try to provide an explanation for this. I believe very strong about it and would like to learn more. If it’s OK, as you attain more intensive wisdom, could you thoughts including more posts very similar to this one with additional info? It will be extremely helpful and useful for me and my colleagues.

    Like

    Reply
  11. poet

    Resources such as the 1 you mentioned here will be extremely useful to myself! I will publish a hyperlink to this page on my personal weblog. I’m certain my site website visitors will locate that very helpful.

    Like

    Reply
  12. Abenteurreisen

    I admire the beneficial facts you provide inside your content articles. I’ll bookmark your weblog and also have my kids verify up right here normally. I am fairly sure they will learn a lot of new stuff here than anyone else!

    Like

    Reply
  13. How to download songs from YouTube

    I must say, as considerably as I enjoyed reading what you had to say, I couldnt help but lose interest after a while. Its as if you had a excellent grasp to the topic matter, but you forgot to include your readers. Perhaps you should think about this from far more than one angle. Or maybe you shouldnt generalise so much. Its better if you think about what others may have to say instead of just going for a gut reaction to the topic. Think about adjusting your own thought process and giving others who may read this the benefit of the doubt.

    Like

    Reply
  14. dayton oh laptop repair

    Dude, please tell me that youre heading to create a lot more. I notice you havent written an additional blog for a while (Im just catching up myself). Your blog is just also important to be missed. Youve received so significantly to say, these knowledge about this subject it would be a shame to see this weblog disappear. The internet needs you, man!

    Like

    Reply
  15. click here

    How is it that just anybody can publish a weblog and get as popular as this? Its not like youve said anything extremely impressive –more like youve painted a pretty picture through an issue that you know nothing about! I dont want to sound mean, here. But do you seriously think that you can get away with adding some pretty pictures and not seriously say something?

    Like

    Reply
  16. Parasols

    How is it that just anyone can publish a weblog and get as popular as this? Its not like youve said something extremely impressive –more like youve painted a fairly picture more than an issue that you know nothing about! I dont want to sound mean, right here. But do you genuinely think that you can get away with adding some fairly pictures and not definitely say anything?

    Like

    Reply
  17. cieaura

    Good day We’re so thrilled I discovered this blog page, I truly located u through accident, whilst I were looking upon Digg for something different, Anyhow We are here and We would certainly want to mention cheers for the remarkable posting plus a all-round enjoyable website Incidentally Lindsey Lohan is in the news again?

    Like

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s