Reputation: 21
I am connection to the Netgroup using FMS (rtmfp). I can connect to the Netgroup (reiceve NetStatusEvent "NetGroup.Connect.Success") but that is all. I can't post anything or see that someone has joined the Netgroup, no NetStatusEvent fires. Am I missing something?
Here is the code:
public function connect(url:String):void {
_nc = new NetConnection();
_nc.client = this;
_nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
_nc.connect(url);
}
private function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code){
case "NetConnection.Connect.Success":
createGroup();
break;
case "NetGroup.Connect.Success":
//post msg to the group
var message:Object = new Object;
message.text = "Hello";
message.sender = _nc.nearID;
_netGroup.post(message);
break;
default:
trace("event.info.code: " + event.info.code);
break;
}
}
private function createGroup():void {
_groupSpecifier = new GroupSpecifier("test_group");
_groupSpecifier.postingEnabled = true;
_groupSpecifier.multicastEnabled = true;
_groupSpecifier.serverChannelEnabled = true;
_netGroup = new NetGroup(_nc, _groupSpecifier.groupspecWithAuthorizations());
_netGroup.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
}
Upvotes: 2
Views: 635
Reputation: 902
In order to complete the task you are asking about you should fire off a post() when you hear "NetGroup.Neighbor.Connect". If you fire your post() right when you connect to the group, you are potentially firing it before you even connect to a neighbor. Connecting you to a group and the groups neighbors does not happen simultaneously. P2P connections rely on the affirmation that a neighbor is available so that messages can propagate throughout the entire group.
To receive a post() message you need to listen for "NetGroup.Posting.Notify". The message you created is an object simply called message with two pieces of information attached. At this point you can take message and print it out to a text field.
I posted some example code (wont work out of the box) based on what you wrote. You can see that we are waiting until we can confirm a connection to a neighbor within the group before a post() is made.
private function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code){
case "NetConnection.Connect.Success":
createGroup();
break;
case "NetGroup.Connect.Success":
connected = true;
break;
case "NetGroup.Neighbor.Connect":
//post msg to the group
var message:Object = new Object;
message.text = "Hello";
message.sender = _nc.nearID;
_netGroup.post(message);
case "NetGroup.Posting.Notify":
writeText(message.sender + ": " message.text);
default:
trace("event.info.code: " + event.info.code);
break;
}
}
There is a lot these classes can do for you so keep going with it. Your not limited to just text messages but you can literally pass any object. Streaming data can be handled but there are specific classes for that.
links that I referenced and will help you further:
Upvotes: 0
Reputation: 11
It could be an issue with the location you have selected to post the message. posting is from Connect.sucess , so the neighbors are not connected to the peer. The message will be posted to other peers only after the neighbor connect.
Upvotes: 1
Reputation: 11
You need to check for "NetGroup.Posting.Notify" in the switch block, and use the post method of the NetGroup class!
private function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code){
case "NetConnection.Connect.Success":
createGroup();
break;
case "NetGroup.Connect.Success":
break;
case "NetGroup.Posting.Notify" :
receiveMessage(event.info.message);
break;
default:
trace("event.info.code: " + event.info.code);
break;
}
}
Then in the receiveMessage function:
private function receiveMessage(message:Object):void
{
trace(message.text)
}
And finally the sending function:
private function sendMessage(txt:String):void
{
var message:Object = new Object();
message.text = txt;
netGroup.post(message);
}
now you can call sendMessage("text") when you press the send button.
Upvotes: 1