Skip navigation
greg_54fr
Currently Being Moderated

Problem with peer to peer chat

Mar 9, 2013 2:17 PM

Hi,

 

I'm a web developper, but I'm a begginer with flex, I need to make a peer to peer video / texe chat.

 

With the Adobe doc, I have made a code that work fine for video and audio, but it don't work for the text chat part.

 

Unfortunately, I don't find any doc in french so if you can help me to solve my problem, it would be fantastic

 

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                                        width="728" height="685" applicationComplete="{init();}" layout="absolute">
  
          <mx:Script>
                    <![CDATA[
                              import mx.core.UIComponent;
                              private var nc:NetConnection;
                              private var rtmfpServer:String = 'rtmfp://stratus.adobe.com/cbd2224f9a56771b3d4d05c3-bd9b549abca2';
                              private var sendNS:NetStream;
                              private var receiveNS:NetStream;
                              private var neerPeerID:String;
  
                              private var cam:Camera;
                              private var mic:Microphone;
  
                              public function init():void {
                                        initCamera();
                                        initNetConnection();
                              }
  
                              private function initCamera():void {
                                        if (Camera.names.length > 0) { 
                                                  cam = Camera.getCamera();
                                                  cam.setMode(320, 240, 15);
                                                  cam.setQuality(0, 90);
  
                                                  my_video_display.attachCamera(cam);
                                        }
  
                                        if (Microphone.names.length > 0) {
                                                  mic = Microphone.getMicrophone();
                                        }
                              }
  
                              private function initNetConnection():void {
                                        nc = new NetConnection();
                                        nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusEvent);
                                        nc.connect(rtmfpServer);
                              }
  
                              private function netStatusEvent(event:NetStatusEvent):void {
                                        debug('NetConnection status event (1): ' + event.info.code);
                                        if (event.info.code == 'NetConnection.Connect.Success') {
                                                  neerPeerID = nc.nearID;
                                                  farPeerId_text.text = neerPeerID;
                                                  initSendNetStream();
  
                                        }
                              }
  
                              private function initSendNetStream():void {
                                        sendNS = new NetStream(nc, NetStream.DIRECT_CONNECTIONS);
                                        sendNS.addEventListener(NetStatusEvent.NET_STATUS, netStatusEvent);
  
                                        var clientObject:Object = new Object();
                                        clientObject.onPeerConnect = function(ns:NetStream):Boolean {return true;}
                                        sendNS.client = clientObject;
  
  
                                        sendNS.attachCamera(cam);
                                        sendNS.attachAudio(mic);
                                        sendNS.publish('video');
                              }
  
                              private function addContact():void {
                                        var nc2:NetConnection = new NetConnection();
                                        nc2.addEventListener(NetStatusEvent.NET_STATUS, function (event:NetStatusEvent):void {
  
                                                  debug('************************');
                                                  debug('NetConnection status event (2): ' + event.info.code);
                                                  debug('contact_peer_id_text.text '+contact_peer_id_text.text);
  
                                                  receiveNS = new NetStream(nc2, contact_peer_id_text.text);
                                                  receiveNS.addEventListener(NetStatusEvent.NET_STATUS, netStatusEvent);


                                                  var video:Video = new Video();
                                                  video.attachNetStream(receiveNS);
                                                  receiveNS.play('video');
  
                                                  receiveNS.client = this; // Ajout
  
                                                  other_video_display.addChild(video);
                                                  contact_peer_id_text.text = '';
                                        });
                                        nc2.connect(rtmfpServer);
                              }
  
                              private function debug(txt:String):void {
                                        txt += '<br>';
                                        chatBox_debug.validateNow();
                                        chatBox_debug.htmlText += txt;
                                        chatBox_debug.validateNow();
                                        chatBox_debug.verticalScrollPosition = chatBox_debug.maxVerticalScrollPosition;
  
                              }
  
                              protected function deconnecter_btn_clickHandler(event:MouseEvent):void
                              {
                                        // TODO Auto-generated method stub
                                        {
                                                  var i:int; 
                                                  debug('sendNS.peerStreams.length ' + sendNS.peerStreams.length);
                                                  if(sendNS != null)
                                                  {
                                                            for(i=0; i<sendNS.peerStreams.length; i++)
                                                            {
                                                                      sendNS.peerStreams[i].close();
                                                            }
                                                  }
                                        }
                              }
  
                              public function envoyer_btn_clickHandler(event:MouseEvent):void
                              {
                                        var messageToSend:String = message_txt.text;
                                        message_txt.text = '';
  
                                        chat_txt.validateNow(); 
                                        sendNS.send("reciveMessage", messageToSend, 0);
  
                                        debug('sendNS.peerStreams.length ' + sendNS.peerStreams.length);


  
                                        messageToSend = messageToSend + '<br>';
                                        chat_txt.htmlText += messageToSend;
  
                                        chat_txt.validateNow(); 
                                        chat_txt.verticalScrollPosition = chat_txt.maxVerticalScrollPosition;
                              }
  
                              public function reciveMessage(vMessage:String, color:uint):void {
                                        debug(vMessage);
                                        chat_txt.htmlText += vMessage;
                                        chat_txt.validateNow();
                                        chat_txt.verticalScrollPosition = chat_txt.maxVerticalScrollPosition;
                              }
  
                    ]]>
          </mx:Script>
  
          <mx:HBox id="video_stack" left="10" top="10">
                    <mx:VBox width="577" height="92" borderColor="#696969" dropShadowVisible="true"
                                         verticalAlign="bottom">
                              <mx:HBox>
                                        <mx:TextInput id="farPeerId_text" width="543" editable="false"
                                                                        text="Your Peer ID is loading..."/>
                              </mx:HBox>
                              <mx:TextInput id="contact_peer_id_text" width="542" editable="true"/>
                              <mx:Button label="Contacter" click="{addContact();}"/>
                    </mx:VBox>
          </mx:HBox>
          <mx:VideoDisplay id="other_video_display" x="10" y="110" width="320" height="240"/>
          <mx:TextArea id="chatBox_debug" x="10" y="549" width="694" height="131"/>
          <mx:Button id="deconnecter_btn" x="342" y="255" width="245" label="Déconnecter"
                                 click="deconnecter_btn_clickHandler(event)"/>
          <mx:VideoDisplay id="my_video_display" x="338" y="109" width="249" height="138"/>
          <mx:TextArea id="chat_txt" x="10" y="369" width="577" height="116"/>
          <mx:TextInput id="message_txt" x="10" y="493" width="458"/>
          <mx:Button id="envoyer_btn" x="476" y="495" width="110" label="Envoyer"
                                 click="envoyer_btn_clickHandler(event)"/>
  
</mx:Application>

 

If you see what is the reason why it don't work, please help me

 

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points