---
title: "FMS案例开发--视频聊天室（二）【转】"
author: "freeroy"
date: "2010-12-25T17:34:21Z"
tags: []
categories: ["编程语言"]
slug: "fms"
draft: false
lastmod: "2010-12-25T17:34:21Z"
id: 851854
url: "https://freeroy.iteye.com/blog/851854"
---

通过[**前面**](http://www.cnblogs.com/beniao/archive/2009/05/09/1453198.html)的简单分析，下面正式进入视频聊天室的设计开发阶段。根据我以前开发管理软件的经验，我们从基础模块开始，首先设计和开发后台功能模块，实现基本的用户注册和通信接口等相关功能。

      聊天室需求简单，主要就一张表用来存储用户注册资料，当用户登陆聊天室的时候，则通过通信接口来验证用户。SQL脚本如下：

&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;USE \[ChatRoom]  
GO  
/\*\*\*\*\** 对象:  Table \[dbo].\[UserInfo]    脚本日期: 05/16/2009 16:45:53 \*\*\*\*\*\*/  
SET ANSI\_NULLS ON  
GO  
SET QUOTED\_IDENTIFIER ON  
GO  
SET ANSI\_PADDING ON  
GO  
CREATE TABLE \[dbo].\[UserInfo](  
    \[ID] \[int] IDENTITY(1,1) NOT NULL,  
    \[UserName] \[varchar](50) NULL,  
    \[NickName] \[varchar](50) NOT NULL,  
    \[Password] \[varchar](50) NOT NULL,  
    \[QQ] \[varchar](20) NULL,  
    \[RegTime] \[datetime] NULL,  
 CONSTRAINT \[PK\_UserInfo] PRIMARY KEY CLUSTERED   
(  
    \[ID] ASC  
)WITH (PAD\_INDEX  = OFF, STATISTICS\_NORECOMPUTE  = OFF, IGNORE\_DUP\_KEY = OFF, ALLOW\_ROW\_LOCKS  = ON, ALLOW\_PAGE\_LOCKS  = ON) ON \[PRIMARY]  
) ON \[PRIMARY]  
GO  
SET ANSI\_PADDING OFF  
GO

 

      本案例开发的通信接口采用WebService提供，考虑到只是做一些基本的通信加上本文重在于演示，所以就不去使用使用诸如FluorineFx之类的通信网关了。如有对FluorineFx感兴趣的朋友可以浏览以下文章：《[Flex与.NET互操作系列文章](http://www.cnblogs.com/beniao/archive/2009/02/13/1385996.html)》

       数据访问通过Linq To Sql实现，对外提供WebService接口，Flex通过WebService和.NET通信达到数据交互的目的。

&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;namespace ChatRoomService  
{  
    /// &lt;summary&gt;  
    /// CRService 的摘要说明  
    /// &lt;/summary&gt;  
    \[WebService(Namespace = "http://tempuri.org/")]  
    \[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1\_1)]  
    \[ToolboxItem(false)]  
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。  
    // \[System.Web.Script.Services.ScriptService]  
    public class CRService : System.Web.Services.WebService  
    {  
        private static ChatRoomDataContext ctx = new ChatRoomDataContext();

        \[WebMethod]  
        public string HelloWorld()  
        {  
            return "Hello World";  
        }  
          
        /// &lt;summary&gt;  
        /// 用户登录  
        /// &lt;/summary&gt;  
        /// &lt;param name="UserName"&gt;&lt;/param&gt;  
        /// &lt;param name="Password"&gt;&lt;/param&gt;  
        /// &lt;returns&gt;&lt;/returns&gt;  
        \[WebMethod]  
        public UserInfo Login(string UserName, string Password)  
        {  
            List&lt;UserInfo&gt; list= ctx.UserInfo.Where(u =&gt; u.UserName == UserName).Where(p =&gt; p.Password == Password).ToList();  
            if (list.Count&gt;0)  
                return list\[0];  
            return null;  
        }

        /// &lt;summary&gt;  
        /// 用户注册  
        /// &lt;/summary&gt;  
        /// &lt;param name="info"&gt;&lt;/param&gt;  
        /// &lt;returns&gt;&lt;/returns&gt;  
        \[WebMethod]  
        public string Register(string infos)  
        {  
            string\[] info = infos.Split('|');  
            UserInfo userInfo = new UserInfo  
            {  
                UserName = info\[0],  
                Password = info\[1],  
                NickName = info\[2],  
                QQ = info\[3],  
                RegTime = DateTime.Now  
            };

            List&lt;UserInfo&gt; list = ctx.UserInfo.Where(n =&gt; n.UserName == userInfo.UserName).ToList();  
            if (list.Count &gt; 0)  
                return string.Format("{0}|{1}", "Error", "用户" + userInfo.UserName + "已经存在");

            try  
            {  
                ctx.UserInfo.InsertOnSubmit(userInfo);  
                ctx.SubmitChanges();  
                return "Success";  
            }  
            catch  
            {  
                return "Error";  
            }  
        }  
    }  
}

 

      由于是直接使用WebService通信，ActionScript对象和C#对象之间的序列化问题，上面就直接以字符传的形式返回。有了通信接口，下面就可以做用户注册和登录的工作了。在Flex客户端全部通过&lt;mx:WebService&gt;组件和WebService通信，注册的完整代码如下：

![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)Flex注册界面完整代码  
&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)&lt;?xml version="1.0" encoding="utf-8"?&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)&lt;mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"   
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)    width="400" height="282" showCloseButton="true"  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)    close="closeWindow(event)" fontSize="12"&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)      
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)    &lt;mx:WebService id="ctService" wsdl="http://localhost:1535/CRService.asmx?wsdl"&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)        &lt;mx:operation name="Register" result="onRegisterResult(event)" fault="onRegisterFaile(event)"&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)        &lt;/mx:operation&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)    &lt;/mx:WebService&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)      
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)    &lt;mx:Script&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)        &lt;![CDATA[  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)            import mx.controls.Alert;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)            import flex.chatroom.vo.UserInfo;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)            import mx.rpc.events.ResultEvent;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)            import mx.managers.PopUpManager;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)              
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)            private function onRegister(evt:MouseEvent):void  
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)            ![](http://www.cnblogs.com/Images/dot.gif){  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                var info:UserInfo = new UserInfo();  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                info.UserName = this.txtUserName.text;  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                info.Password = this.txtPassword.text;  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                info.NickName = this.txtNickName.text;  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                info.QQ = this.txtQQ.text;  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                var infos:String=info.UserName+"|"+info.Password+"|"+info.NickName+"|"+info.QQ;  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                this.ctService.Register(infos);  
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)            }  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)              
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)            private function onRegisterResult(evt:ResultEvent):void  
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)            ![](http://www.cnblogs.com/Images/dot.gif){  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                var result:String = evt.result.toString();  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                var arr:Array = result.split('|');  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                if(arr.length&gt;1)  
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)                ![](http://www.cnblogs.com/Images/dot.gif){  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                    Alert.okLabel="确 定";  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                    Alert.show(arr\[1],"系统提示");  
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                if(arr\[0]=="Success")  
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)                ![](http://www.cnblogs.com/Images/dot.gif){  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                    Alert.okLabel="确 定";  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                    Alert.show("恭喜你,注册成功！","系统提示");  
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                else  
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)                ![](http://www.cnblogs.com/Images/dot.gif){  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                    Alert.okLabel="确 定";  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                    Alert.show("注册失败,请重试！","系统提示");  
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)            }  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)              
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)            private function onRegisterFaile(evt:Event):void  
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)            ![](http://www.cnblogs.com/Images/dot.gif){  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                Alert.okLabel="确 定";  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                Alert.show("注册失败,请重试！","系统提示");  
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)            }  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)              
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)            private function closeWindow(evt:Event):void  
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)            ![](http://www.cnblogs.com/Images/dot.gif){  
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                PopUpManager.removePopUp(this);  
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)            }  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)        ]]&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)    &lt;/mx:Script&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)      
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)    &lt;mx:Form y="2" x="2" width="99%"&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)        &lt;mx:FormHeading label="新用户注册"/&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)        &lt;mx:FormItem label="用户名："&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)            &lt;mx:TextInput id="txtUserName" /&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)        &lt;/mx:FormItem&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)        &lt;mx:FormItem label="密  码："&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)            &lt;mx:TextInput id="txtPassword" /&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)        &lt;/mx:FormItem&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)        &lt;mx:FormItem label="呢  称："&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)            &lt;mx:TextInput id="txtNickName" /&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)        &lt;/mx:FormItem&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)        &lt;mx:FormItem label="Q  Q："&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)            &lt;mx:TextInput id="txtQQ" /&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)        &lt;/mx:FormItem&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)        &lt;mx:FormItem&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)            &lt;mx:HBox&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)                &lt;mx:Button label="注 册" click="onRegister(event)" fontWeight="normal" width="95"/&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)                &lt;mx:Button label="取 消" click="closeWindow(event)" fontWeight="normal"/&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)            &lt;/mx:HBox&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)        &lt;/mx:FormItem&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)    &lt;/mx:Form&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)      
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)&lt;/mx:TitleWindow&gt;  
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)

 

                  ![](http://images.cnblogs.com/cnblogs_com/beniao/csharpseven/ChatRoomRegister.jpg) 

      用户登录同样是使用WebService通信进行数据库验证，实现代码大致如下：

&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;&lt;mx:WebService id="ctService" wsdl="http://localhost:1535/CRService.asmx?wsdl"&gt;  
    &lt;mx:operation name="Login" result="onLoginRusult(event)" fault="onLoginFaile(event)"&gt;  
    &lt;/mx:operation&gt;  
&lt;/mx:WebService&gt;

private function onLogin():void  
{

    this.ctService.Login(this.txtUserName.text,this.txtPassword.text);  
}

private function onLoginRusult(evt:ResultEvent):void  
{  
    if(evt.result != null)  
    {  
        //显示自己的视频  
        //displayCameraSelf();  
          
        //连接FMS服务器发布自己的视频流  
        publishNc = new NetConnection();  
        publishNc.connect("rtmp://localhost/ChatRoom");  
        so = SharedObject.getRemote("ChatSO",publishNc.uri,false);  
        publishNc.addEventListener(NetStatusEvent.NET\_STATUS,onNetStatusHandler);  
        so.addEventListener(SyncEvent.SYNC,onSyncHandler);      
        so.connect(publishNc);                  
          
        myUserName = evt.result.UserName;  
        myNickName = evt.result.NickName;  
          
        this.loginPanel.visible = false;  
        this.chatRoomPanel.visible=true;  
        ChatRoomViewStack.selectedChild = chatRoomPanel;  
    }  
    else  
    {  
        Alert.okLabel="确 定";  
        Alert.show("登录失败！","系统提示");  
    }  
}

 

                    ![](http://images.cnblogs.com/cnblogs_com/beniao/csharpseven/ChatRoomLogin.jpg)

      通信主要就这两个功能点，都比较简单，关于Flex与WebServie通信我以前也写过两篇文章在《[Flex与.NET互操作系列文章](http://www.cnblogs.com/beniao/archive/2009/02/13/1385996.html)》里，不熟悉的的朋友可以先看看通信方面的实现。

      本文就先介绍到这里，下一篇将介绍聊天室的详细开发，包括视频、语音、文字聊天的实现等相关知识点。