Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

Peer-to-Peer Chat with NetGroup in Flash Player 10.1

0 (0 Likes / 0 Dislikes)
[ADOBE DEVELOPER CONNECTION] Hello, my name is Tom, and I am one of the members of Platform Evangelism team. Today I am going to show you how you can build simple peer-to-peer chat. So what do you have to do for this? First of all, you need to get the beta developer key from the Stratus labs page. So we just go here and click "Signup for a Stratus beta developer key" and you get it. I've already got one here, so what I'm going to do-- I'm just going to work with it already. So first of all, you have to lay out some components, so it actually looks like chat, you know? So I have here my text history and also text message field, and a sending button. So when you go to the source code, it looks like this, and we can see that it's a server which is pointing to with RTMFP protocol. And then, we also include our developer key here-- like that. So first of all, we have to connect to the server, so this is what we do by defining function connect, and also initializing netConnection, so we create a variable netConnection of type NetConnection, obviously, and we just initialize it, new NetConnection-- like this. And now, we just register the Listener for it, NET_STATUS event, like this: (NetStatusEvent.NET_STATUS,netStatus); We have to create a function as well: function netStatus(event:NetStatusEvent):void{ and we just include a NetStatus event like this. So finally, we are in NetStatus, and we just write a message we get from the NetStatus, so it's (; And the next thing we are actually going to do--a connection to the server, so like this: netConnection.connect and we include (SERVER+DEVKEY); like this. So once we are in NetStatus, we just switch the (; and in case it's "NetConnection.Connect.Success": which means we are successfully connected to the Stratus service. We just set up the group like this: setupGroup(); and we also break it. So now we have to add the setupGroup function setupGroup():void{ Well, we do two things. First of all, we have to define so-called Group Specifier, which defines what you can actually do with the NetGroup. It's type GroupSpecifier, and we initialize it and we include a group name, like this--so "myGroup/g0); And so we also enable the posting, so posting means that you can actually broadcast message to the participants of the group. So we change it to true: postingEnabled = true and we also enable server channel: serverChannelEnabled = true which means that this essential thing for the communication which enables you to work with the server actually. So the next thing we have to do here is define the actual NetGroup of type NetGroup. Now, again, we initialize it, we connect to netGroup netConnection, and we also pass this spec: groupspecWithAuthorizations()); There is also a difference between groupspecWithAuthorizations and WithoutAuthorizations, but you don't have to care about this now-- it's all about the security of the groups. And we also register the Listener: addEventListener(NetStatusEvent.NET_STATUS,netStatus); like this. That's pretty much it--now we have to do 2 more functions. One of them is going to send a message to the group and one of them is going to receive the message from the group. So how does it work? First of all, we create function called sendMessage, and we'll pass a string to it like this: (txt:String):void{ We have to cross-track to the message object: so we just write: var message:Object = new Object(); And message object should contain some information about a text and also some information about a user so we pass userName, which is going to be the txtUser.text; and also some sort of id, so we can actually-- we would like to connect or send a direct message to the sender who actually broadcasted his message, so if you want to do that, you can include sender id, and it's going to be netConnection.nearID You can also convert this nearID to groupID, like that, which is pretty useful. netGroup.convertPeerIDToGroupAddress(netConnection.nearID); to group address--like this. So finally, we are pretty much done. We just post the message to the new group, and now we are going to receive it, so we define function receiveMessage it's going to handle that (message:Object):void{ And finally, we just writeText(message userName+= txt+"n/"; Yeah, so you can see that this is what we have already defined, and "+message.text); like that. Also, the one thing--you actually broadcast the message to the group, but you don't receive it, so what you have to do here is receiveMessage(message); like that. Okay, we are pretty much done. The last thing we have to do here is actually receive the message, and you actually receive the message in NetStatus. So this is what happens in a case when NetGroup posting gets notified: "NetGroup.Posting.Notify": Something like that--yeah. So "NetGroup.Posting.Notify" and we pass to the receiveMessage function, we pass this (; variable like that. Also, the last thing to make it more useful and more prettier to the application itself, you can also handle "NetGroup.Connect. Success" as well, and for instance-- like switch connected to true, like that. So now it's actually connected with this button here, so it got enabled when the connected is true--by default, it's false. So at the final state, we are going to test the application and see if it works, so we go and just press the Run button, and we write a message and we hit Send, and we can see now that it's received, so we can do from the outer window--so the way back--it works as well. Okay, and that's pretty much it, so I hope it was easy to use and I'm actually looking forward to seeing more and more applications using peer-to-peer in the future. If you want, you can go to my blog and see more related articles on this topic. Thank you. [ADOBE DEVELOPER CONNECTION]

Video Details

Duration: 8 minutes and 45 seconds
Language: English
License: All rights reserved
Genre: None
Views: 140
Posted by: adobetv on Oct 21, 2010

In this video, Platform Evangelist Tom Krcha discusses how to create a simple peer-to-peer chat using group posting with Adobe Stratus and the Flash Platform.

Caption and Translate

    Sign In/Register for Dotsub to translate this video.