转载【p2p持续开发教程2】P2P应用之文本聊天
flash media server 4 安装:http://uh.9ria.com/link.php?url= ... d.php%3Ftid%3D99635
flash cs5.5 &&fb4.5安装:http://uh.9ria.com/link.php?url= ... .php%3Ftid%3D100325
【p2p持续开发教程1】关于P2P应用中定向路径的解释:http://uh.9ria.com/link.php?url= ... .php%3Ftid%3D100022
【p2p持续开发教程3】p2p应用开发之电子共享白板http://uh.9ria.com/link.php?url= ... d.php%3Ftid%3D85173
先把代码放上吧。
- package {
- import flash.display.Sprite;
- import flash.events.TextEvent;
- import flash.events.MouseEvent;
- import flash.events.NetStatusEvent;
- import fl.events.ComponentEvent;
- import flash.net.*;
- public class main extends Sprite{
- private var netConnection:NetConnection = null;
- private var netGroup:NetGroup = null;
- //如果你有装fms的话可以用下面的地址。。
- //private const SERVER:String = "rtmfp://202.195.73.145/NetGroupPostExample";
- //没装的话用这个开发key
- private const StratusAddress:String = "rtmfp://stratus.adobe.com";
- private const DeveloperKey:String = "1710124cbf69e3f25b780c13-d6cbf2cb35a1";
- //如果你想要试下局域网的话。。也就不用fms服务器和adobe的开发key,可以这样netConnection.connect("rtmfp:");
- public function main(){
- //这是界面上的登录确定按钮
- screen.login_btn.addEventListener(MouseEvent.CLICK,login_click);
- //聊天输入昵称文本框
- screen.name_txt=="请输入您的昵称";
- }
- private function login_click(e:MouseEvent):void{
- screen.visible=false;
- connect();
- }
- private function connect():void{
- netConnection = new NetConnection();
- netConnection.addEventListener(NetStatusEvent.NET_STATUS, netConnectionHandler);
- netConnection.connect(StratusAddress + "/" + DeveloperKey);
- //发送消息可以是回车,或是点击发送按钮
- write_txt.addEventListener(ComponentEvent.ENTER, DoPost);
- enter_btn.addEventListener(MouseEvent.CLICK,enter_click);
- }
- private function netConnectionHandler(e:NetStatusEvent):void{
- switch (e.info.code)
- {
- //链接成功后的初始化
- case "NetConnection.Connect.Success":
- name_input.text=screen.name_txt.text;
- //初始化登陆者
- onConnecter();
- break;
- case "NetConnection.Connect.Closed":
- break;
- case "NetStream.Connect.Success":
- break;
- case "NetGroup.Posting.Notify": // e.info.message, e.info.messageID
- OnPosting(e.info.message);
- trace(e.info.message.user);
- break;
- case "NetConnection.Connect.Failed":
- trace("错误","错误")
- break;
- case "NetStream.Connect.Closed":
- break;
- }
- }
- private function onConnecter():void{
- var groupSpecifier:GroupSpecifier;
- groupSpecifier = new GroupSpecifier("aslrexample");
- groupSpecifier.postingEnabled = true;
- groupSpecifier.serverChannelEnabled = true;
- //用一个群组,使效率更好点
- netGroup = new NetGroup(netConnection, groupSpecifier.groupspecWithAuthorizations());
- netGroup.addEventListener(NetStatusEvent.NET_STATUS, netConnectionHandler);
- StatusMessage("【系统信息】" + screen.name_txt.text+ "@ 加入聊天室\n");
- }
- private function DoPost(e:ComponentEvent):void{
- mes();
- }
- private function enter_click(e:MouseEvent):void{
- mes();
- }
- private function mes():void{
- var message:Object = new Object;
- message.text = write_txt.text;
- message.user=screen.name_txt.text;
- message.destination = netGroup.convertPeerIDToGroupAddress(netConnection.nearID);
- netGroup.post(message);
- //netGroup.sendToNearest(message.text, message.destination);
- //netGroup.sendToNeighbor(message,"27249");
- StatusMessage("【我】 说: " + write_txt.text + "\n");
- write_txt.text="";
- }
- private function OnPosting(message:Object):void{
- StatusMessage("【群】" + message.user + "@对大家说: " + message.text + "\n");
- }
- private function StatusMessage(msg:Object):void{
- chat_txt.text += msg;
- }
- }
- }
我用的是cs5.5的版本。。可能有些朋友会打不开。。
如果你想要试下局域网的话。。也就不用fms服务器和adobe的开发key,可以这样netConnection.connect("rtmfp:");
-
gz.jpg (72.91 KB)
工作区界面
-
gy.jpg (42.93 KB)
登录界面
-
gg$D.jpg (42.36 KB)
进入聊天
-
82U`8W~LU}ZIM8~V]{F$L`W.jpg (92.29 KB)
两个客户端交流
-
test.rar (841.65 KB)
原文地址:http://bbs.9ria.com/thread-100339-1-1.html
人生没有失败,只有自我放弃;只要不放弃,梦想总有一天会变成现实。