- 浏览: 39059 次
- 性别:
- 来自: 深圳
文章分类
最新评论
package
{
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.ObjectEncoding;
import flash.media.Camera;
import flash.net.NetStream;
import flash.media.SoundTransform;
import flash.media.Microphone;
//[SWF(backgroundColor = 0xffffff,width = 1003,height = 527)]
public class Main extends Sprite
{
private var server:String = "rtmfp://p2p.rtmfp.net/";
private const DeveloperKey:String = "9bb73c685f27383f07976dbb-60df5c2574da/twomen";
private var chatPanel;
private var controlStream:NetStream;
private var outStream:NetStream;
private var inStream:NetStream;
private var listenerStream:NetStream;
private var yourName:String;
private var yourId:String;
private var oppName:String;
private var oppId:String;
private var nc;
private var cam;
private var mic;
public function Main():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
Init();
}
private function Init()
{
showMes("我要开始了!");
chatPanel=new ChatPanel();
chatPanel.x = stage.stageWidth / 2 - chatPanel.di.width / 2;
chatPanel.y = stage.stageHeight / 2 - chatPanel.di.height / 2;
addChild(chatPanel);
chatPanel.linkBtn.label = "Connect";
chatPanel.sendBtn.label = "Send";
chatPanel.acceptBtn.label = "Accept";
chatPanel.callBtn.label = "Call";
chatPanel.linkBtn.addEventListener(MouseEvent.CLICK,clickHandler);
startVideo();
}
private function clickHandler(e:MouseEvent)
{
yourName = chatPanel.myName.text;
nc=new NetConnection();
nc.client = this;
nc.objectEncoding = ObjectEncoding.AMF3;
nc.addEventListener(NetStatusEvent.NET_STATUS,statusEvent);
nc.connect(server+DeveloperKey);
chatPanel.sendBtn.addEventListener(MouseEvent.CLICK,sendChatMsg);
}
private function statusEvent(e:NetStatusEvent)
{
//trace(e.info.code);
var code = e.info.code;
switch (code)
{
case "NetConnection.Connect.Success" :
chatPanel.myId.text = nc.nearID;
yourId = nc.nearID;
chatPanel.callBtn.addEventListener(MouseEvent.CLICK,startCall);
planStream();//准备流
break;
default :
break;
}
}
private function listenerHandler(e:NetStatusEvent)
{
trace(e.info.code);
}
private function inHandler(e:NetStatusEvent)
{
trace(e.info.code);
}
private function outHandler(e:NetStatusEvent)
{
trace("onInCall"+e.info.code);
outStream.send("onInCall",chatPanel.myName.text);
}
private function controlHandler(e:NetStatusEvent)
{
trace(e.info.code);
}
private function startCall(e:MouseEvent)
{
trace("你要开始call了啊。。");
oppName = chatPanel.oppName.text;
oppId = chatPanel.oppId.text;
placeCall(oppName,oppId);
}
private function placeCall(tmpOppName:String,tmpOppId:String)
{
chatPanel.msg.text = "正在呼叫:" + tmpOppName + "...";
controlStream = new NetStream(nc,tmpOppId);
controlStream.addEventListener(NetStatusEvent.NET_STATUS,controlHandler);
controlStream.play("control"+tmpOppName);
outStream = new NetStream(nc,NetStream.DIRECT_CONNECTIONS);
outStream.addEventListener(NetStatusEvent.NET_STATUS,outHandler);
outStream.publish("media-caller");
var outObj=new Object();
outStream.client = outObj;
outObj.onPeerConnect=function(tmpNs:NetStream)
{
chatPanel.msg.text="正在建立P2P连接……";
};
startAudio();
startVideo();
//接受来自外部的丶对丶视频
inStream = new NetStream(nc,tmpOppId);
inStream.addEventListener(NetStatusEvent.NET_STATUS,inHandler);
inStream.play("media-callee");
var st = new SoundTransform(50);
inStream.soundTransform = st;
//被呼叫方接受连接时触发该事件
var inObj=new Object();
inStream.client = inObj;
inObj.onCallAccepted=function(tmpOppName:String)
{
showMes("已经接受了你的呼叫");
chatPanel.msg.text=tmpOppName+"已经接受了你的呼叫……";
}
;
inObj.onIm=function(userName:String,chatMsg:String)
{
chatPanel.chatContent.text+=userName+":"+chatMsg+"\n";
chatPanel.chatContent.verticalScrollPosition=chatPanel.chatContent.textHeight;
};
chatPanel.remoteVideoDisplay.attachNetStream(inStream);
}
private function acceptCall(e:MouseEvent)
{
chatPanel.msg.text = "你已经接受了对方的呼叫……";
inStream.receiveAudio(true);
inStream.receiveVideo(true);
chatPanel.remoteVideoDisplay.attachNetStream(inStream);
outStream = new NetStream(nc,NetStream.DIRECT_CONNECTIONS);
outStream.addEventListener(NetStatusEvent.NET_STATUS,outHandler);
outStream.publish("media-callee");
var outObj=new Object();
outStream.client = outObj;
outObj.onPeerConnect=function (tmpNs:NetStream)
{
trace(tmpNs.farID);
};
showMes("请问,请问,到这来了么");
outStream.send("onCallAccepted",chatPanel.myName.text);
startVideo();
startAudio();
}
private function planStream()
{
listenerStream = new NetStream(nc,NetStream.DIRECT_CONNECTIONS);
listenerStream.addEventListener(NetStatusEvent.NET_STATUS,listenerHandler);
listenerStream.publish(("control"+yourName));
var listenerObj=new Object();
listenerStream.client = listenerObj;
listenerObj.onPeerConnect=function(tmpNs:NetStream)
{
showMes("我这个是在哪里呀。。。");
var caller=tmpNs;
inStream=new NetStream(nc,caller.farID);
inStream.addEventListener(NetStatusEvent.NET_STATUS,inHandler);
inStream.play("media-caller");
var st:SoundTransform=new SoundTransform(50);
inStream.soundTransform=st;
inStream.receiveAudio(false);
inStream.receiveVideo(false);
var inObj=new Object();
inStream.client = inObj;
showMes("为什么啊");
inObj.onInCall=function(tmpOppName:String)
{
showMes("为什么啊,为什么啊啊");
chatPanel.msg.text=tmpOppName+"正在呼叫你,你接受吗?";
chatPanel.acceptBtn.addEventListener(MouseEvent.CLICK,acceptCall);
}
showMes("一个一个滴");
inObj.onIm=function(userName:String,chatMsg:String)
{
chatPanel.chatContent.text=chatPanel.chatContent.text+(userName+":"+chatMsg+"\n");
chatPanel.chatContent.verticalScrollPosition=chatPanel.chatContent.textHeight;
}
};
}
private function startVideo()
{
cam = Camera.getCamera();
if (cam!=null)
{
cam.setMode(chatPanel.localVideoDisplay.width,chatPanel.localVideoDisplay.height,10);
cam.setQuality(0,80);
chatPanel.localVideoDisplay.attachCamera(cam);
if (outStream)
{
outStream.attachCamera(cam);
}
}
}
private function startAudio()
{
var myMic:Microphone = Microphone.getMicrophone();
if (myMic!=null)
{
if (outStream)
{
outStream.attachAudio(myMic);
}
}
}
//***************************发送聊天信息***************************
private function sendChatMsg(e:MouseEvent)
{
showMes("发送发送");
var tmpMsg = chatPanel.chat.text;
if (tmpMsg.length <= 1)
{
chatPanel.chatContent.appendText("你发送内容为空"+"\n");
chatPanel.chat.text = "";
return;
}
if (outStream)
{
chatPanel.chatContent.text = chatPanel.chatContent.text + chatPanel.myName.text + ":" + tmpMsg + "\n";
outStream.send("onIm",chatPanel.myName.text,tmpMsg);
chatPanel.chat.text = "";
}
else
{
chatPanel.chatContent.appendText("你连接尚未建立"+"\n");
chatPanel.chat.text = "";
}
}
private function showMes(str:String)
{
txt.appendText(str+"\n");
}
}
}
{
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.ObjectEncoding;
import flash.media.Camera;
import flash.net.NetStream;
import flash.media.SoundTransform;
import flash.media.Microphone;
//[SWF(backgroundColor = 0xffffff,width = 1003,height = 527)]
public class Main extends Sprite
{
private var server:String = "rtmfp://p2p.rtmfp.net/";
private const DeveloperKey:String = "9bb73c685f27383f07976dbb-60df5c2574da/twomen";
private var chatPanel;
private var controlStream:NetStream;
private var outStream:NetStream;
private var inStream:NetStream;
private var listenerStream:NetStream;
private var yourName:String;
private var yourId:String;
private var oppName:String;
private var oppId:String;
private var nc;
private var cam;
private var mic;
public function Main():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
Init();
}
private function Init()
{
showMes("我要开始了!");
chatPanel=new ChatPanel();
chatPanel.x = stage.stageWidth / 2 - chatPanel.di.width / 2;
chatPanel.y = stage.stageHeight / 2 - chatPanel.di.height / 2;
addChild(chatPanel);
chatPanel.linkBtn.label = "Connect";
chatPanel.sendBtn.label = "Send";
chatPanel.acceptBtn.label = "Accept";
chatPanel.callBtn.label = "Call";
chatPanel.linkBtn.addEventListener(MouseEvent.CLICK,clickHandler);
startVideo();
}
private function clickHandler(e:MouseEvent)
{
yourName = chatPanel.myName.text;
nc=new NetConnection();
nc.client = this;
nc.objectEncoding = ObjectEncoding.AMF3;
nc.addEventListener(NetStatusEvent.NET_STATUS,statusEvent);
nc.connect(server+DeveloperKey);
chatPanel.sendBtn.addEventListener(MouseEvent.CLICK,sendChatMsg);
}
private function statusEvent(e:NetStatusEvent)
{
//trace(e.info.code);
var code = e.info.code;
switch (code)
{
case "NetConnection.Connect.Success" :
chatPanel.myId.text = nc.nearID;
yourId = nc.nearID;
chatPanel.callBtn.addEventListener(MouseEvent.CLICK,startCall);
planStream();//准备流
break;
default :
break;
}
}
private function listenerHandler(e:NetStatusEvent)
{
trace(e.info.code);
}
private function inHandler(e:NetStatusEvent)
{
trace(e.info.code);
}
private function outHandler(e:NetStatusEvent)
{
trace("onInCall"+e.info.code);
outStream.send("onInCall",chatPanel.myName.text);
}
private function controlHandler(e:NetStatusEvent)
{
trace(e.info.code);
}
private function startCall(e:MouseEvent)
{
trace("你要开始call了啊。。");
oppName = chatPanel.oppName.text;
oppId = chatPanel.oppId.text;
placeCall(oppName,oppId);
}
private function placeCall(tmpOppName:String,tmpOppId:String)
{
chatPanel.msg.text = "正在呼叫:" + tmpOppName + "...";
controlStream = new NetStream(nc,tmpOppId);
controlStream.addEventListener(NetStatusEvent.NET_STATUS,controlHandler);
controlStream.play("control"+tmpOppName);
outStream = new NetStream(nc,NetStream.DIRECT_CONNECTIONS);
outStream.addEventListener(NetStatusEvent.NET_STATUS,outHandler);
outStream.publish("media-caller");
var outObj=new Object();
outStream.client = outObj;
outObj.onPeerConnect=function(tmpNs:NetStream)
{
chatPanel.msg.text="正在建立P2P连接……";
};
startAudio();
startVideo();
//接受来自外部的丶对丶视频
inStream = new NetStream(nc,tmpOppId);
inStream.addEventListener(NetStatusEvent.NET_STATUS,inHandler);
inStream.play("media-callee");
var st = new SoundTransform(50);
inStream.soundTransform = st;
//被呼叫方接受连接时触发该事件
var inObj=new Object();
inStream.client = inObj;
inObj.onCallAccepted=function(tmpOppName:String)
{
showMes("已经接受了你的呼叫");
chatPanel.msg.text=tmpOppName+"已经接受了你的呼叫……";
}
;
inObj.onIm=function(userName:String,chatMsg:String)
{
chatPanel.chatContent.text+=userName+":"+chatMsg+"\n";
chatPanel.chatContent.verticalScrollPosition=chatPanel.chatContent.textHeight;
};
chatPanel.remoteVideoDisplay.attachNetStream(inStream);
}
private function acceptCall(e:MouseEvent)
{
chatPanel.msg.text = "你已经接受了对方的呼叫……";
inStream.receiveAudio(true);
inStream.receiveVideo(true);
chatPanel.remoteVideoDisplay.attachNetStream(inStream);
outStream = new NetStream(nc,NetStream.DIRECT_CONNECTIONS);
outStream.addEventListener(NetStatusEvent.NET_STATUS,outHandler);
outStream.publish("media-callee");
var outObj=new Object();
outStream.client = outObj;
outObj.onPeerConnect=function (tmpNs:NetStream)
{
trace(tmpNs.farID);
};
showMes("请问,请问,到这来了么");
outStream.send("onCallAccepted",chatPanel.myName.text);
startVideo();
startAudio();
}
private function planStream()
{
listenerStream = new NetStream(nc,NetStream.DIRECT_CONNECTIONS);
listenerStream.addEventListener(NetStatusEvent.NET_STATUS,listenerHandler);
listenerStream.publish(("control"+yourName));
var listenerObj=new Object();
listenerStream.client = listenerObj;
listenerObj.onPeerConnect=function(tmpNs:NetStream)
{
showMes("我这个是在哪里呀。。。");
var caller=tmpNs;
inStream=new NetStream(nc,caller.farID);
inStream.addEventListener(NetStatusEvent.NET_STATUS,inHandler);
inStream.play("media-caller");
var st:SoundTransform=new SoundTransform(50);
inStream.soundTransform=st;
inStream.receiveAudio(false);
inStream.receiveVideo(false);
var inObj=new Object();
inStream.client = inObj;
showMes("为什么啊");
inObj.onInCall=function(tmpOppName:String)
{
showMes("为什么啊,为什么啊啊");
chatPanel.msg.text=tmpOppName+"正在呼叫你,你接受吗?";
chatPanel.acceptBtn.addEventListener(MouseEvent.CLICK,acceptCall);
}
showMes("一个一个滴");
inObj.onIm=function(userName:String,chatMsg:String)
{
chatPanel.chatContent.text=chatPanel.chatContent.text+(userName+":"+chatMsg+"\n");
chatPanel.chatContent.verticalScrollPosition=chatPanel.chatContent.textHeight;
}
};
}
private function startVideo()
{
cam = Camera.getCamera();
if (cam!=null)
{
cam.setMode(chatPanel.localVideoDisplay.width,chatPanel.localVideoDisplay.height,10);
cam.setQuality(0,80);
chatPanel.localVideoDisplay.attachCamera(cam);
if (outStream)
{
outStream.attachCamera(cam);
}
}
}
private function startAudio()
{
var myMic:Microphone = Microphone.getMicrophone();
if (myMic!=null)
{
if (outStream)
{
outStream.attachAudio(myMic);
}
}
}
//***************************发送聊天信息***************************
private function sendChatMsg(e:MouseEvent)
{
showMes("发送发送");
var tmpMsg = chatPanel.chat.text;
if (tmpMsg.length <= 1)
{
chatPanel.chatContent.appendText("你发送内容为空"+"\n");
chatPanel.chat.text = "";
return;
}
if (outStream)
{
chatPanel.chatContent.text = chatPanel.chatContent.text + chatPanel.myName.text + ":" + tmpMsg + "\n";
outStream.send("onIm",chatPanel.myName.text,tmpMsg);
chatPanel.chat.text = "";
}
else
{
chatPanel.chatContent.appendText("你连接尚未建立"+"\n");
chatPanel.chat.text = "";
}
}
private function showMes(str:String)
{
txt.appendText(str+"\n");
}
}
}
- chat.rar (975.2 KB)
- 下载次数: 27
相关推荐
在本文中,我们将深入...通过以上步骤,我们可以构建一个功能完备的C# P2P语音聊天系统。这个系统不仅适用于局域网内的通信,也可以通过NAT穿透技术实现广域网的连接,为用户提供便捷的、无需服务器的语音聊天体验。
在IT行业中,语音聊天动态库(P2P)是一种关键的技术组件,特别是在实时通信和社交应用中。这种动态库主要用于实现用户之间的网络语音聊天功能,它利用了P2P(peer-to-peer)技术来高效地传输音频数据。下面将详细...
Adobe Stratus是一个由Adobe公司提供的免费服务,它利用...总之,通过Adobe Stratus、PHP和MySQL的集成,我们可以创建一个高效、可扩展的语音视频聊天应用,充分利用P2P技术降低服务器压力,同时提供丰富的用户体验。
P2P聊天软件是这种技术的一个典型应用,用户可以直接与其他用户交换消息,包括文字和语音。本项目以C++编程语言为基础,利用VC6(Visual C++ 6.0)开发环境实现了这样一个功能齐全的P2P聊天软件。 首先,让我们详细...
在本文中,我们将深入探讨如何仿照YY实现一个P2P语音聊天系统。YY是一款流行的实时通讯软件,尤其以其高质量的语音聊天功能而闻名。在我们的实现过程中,我们将关注几个关键的技术点,包括音频采样率、帧大小、数据...
1、即时视频语音直播 2、视频状态检测 3、局域网内的P2P直播,不消耗任何外网带宽 4、可设置的视频清晰度和缓冲时间 5、无延时的即时文字和表情传输 6、礼物/消费/主播结算/房间类型等功能 7、播放录像功能 Feelink...
综上所述,"c#实现voicechat_p2p点对点语音对讲程序.rar"这个项目涵盖了C#与Windows音频系统交互、音频编码解码、网络编程等多个技术领域,是学习和实践这些技术的一个良好平台。通过深入理解并实现这个程序,开发者...
6. **用户界面与交互**:良好的用户体验是聊天系统的关键,需要开发直观易用的GUI(图形用户界面),支持文本、图片、语音甚至视频聊天,并考虑跨平台兼容性。 7. **容错与稳定性**:P2P网络中的节点可能会频繁加入...
本文设计和实现了一个基于P2P技术的语音聊天工具,以解决传统网络架构中服务器压力的问题,并且实现了一个高效、可靠的语音聊天工具。该工具可以应用于各种领域,例如社交媒体、远程教育、企业内部交流等。
QQ作为中国最流行的IM软件之一,其核心功能包括一对一聊天、群组聊天、文件传输等,深受用户喜爱。仿QQ即时通讯P2P聊天软件的设计和实现,旨在学习和理解即时通讯系统的基本架构和关键技术。 即时通讯系统的构建...
1. **点对点单人聊天**:在P2P系统中,每个节点都可以直接与其他节点通信。当用户A想要向用户B发送消息时,A的节点会创建一个Socket连接到B的节点。数据通过`Socket.getOutputStream()`写入,然后通过`Socket....
P2P聊天应用应提供直观的用户界面,支持文字、语音、视频等多种交流方式,并具备好友管理、群组聊天等功能。 9. **系统性能与稳定性**:由于P2P网络的动态性,系统需要能够应对节点的加入、离开和网络波动。负载...
6. **P2P语音聊天**:P2P(Peer-to-Peer)语音聊天是一种无需中心服务器的通信方式,两台设备直接建立连接交换数据。P2P通信减少了服务器负载,但需要解决网络拓扑和NAT穿透问题,如ICE(Interactive Connectivity ...
本项目是利用C#实现的一个局域网内的语音聊天程序,允许用户进行实时的点对点语音交流。点对点通信(Peer-to-Peer, P2P)是一种网络架构,其中每个参与者都既是客户端又是服务器,直接与其他参与者交换数据,减少了...
UDP(User Datagram Protocol)是一种无连接的、不可靠的传输协议,适合于对实时性要求高的场景,如语音通话、视频会议等。在NAT(Network Address Translation)环境中,由于内网设备的公网IP无法被外部网络直接...
【实现点到点的语音聊天系统】是一种技术性较强的任务,涉及到多个IT领域的知识点,包括网络通信、音频处理、实时传输协议(RTP)、信号编码等。以下将详细阐述这些关键概念。 首先,**网络通信**是基础。点对点...
非C/S模式的P2P(Peer-to-Peer)聊天程序是一种去中心化的通信系统,与传统的客户端/服务器(Client/Server,C/S)架构不同。在C/S模式中,客户端连接到服务器进行数据交换,而P2P网络中,每个参与者既是客户端也是...
UDP则更轻量级,无序传输但速度更快,适合实时通信如语音或视频聊天。 4. **加密通信**:为了保护用户隐私,局域网P2P聊天通常会采用加密技术,如SSL/TLS或AES,对传输的数据进行加密,防止被第三方窃听或篡改。 5...
本文的主要目的是设计和实现一个基于P2P局域网的视频聊天系统,以满足当前网络应用的需求。该系统的设计采用P2P结构,使用C#编程语言在.NET开发环境下进行开发。系统的主要功能包括文字聊天、文件传输、语音聊天和...