加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

[FMS]一步一步学Flash Media Server(五)

发布时间:2020-12-15 18:22:35 所属栏目:百科 来源:网络整理
导读:今天我们的讲解的是在昨天代码的功能上加上在线列表的功能,同时会去掉共享对象,用另一种方法向客户端发消息. 先看看服务端代码我们更改些什么代码: application.onAppStart? = ?function()?{ ???? this .chatMsgArray? ? new ?Array(); ???? .userListArray?

今天我们的讲解的是在昨天代码的功能上加上在线列表的功能,同时会去掉共享对象,用另一种方法向客户端发消息.

先看看服务端代码我们更改些什么代码:

application.onAppStart? = ?function()?{
????
this .chatMsgArray? ? new ?Array();
????
.userListArray? ?Array();
}

application.onConnect?
?function(client,?userName)?{
????
if (checkOnline(userName)){
????????
.rejectConnection(client);
????????
return ;
????}
????
.acceptConnection(client);
????client.userName?
?userName;
????
.userListArray.push(userName);
????sendUserList();
????
// 客户端调用方法
????client.getMsg? ?function(){
????????
?application.chatMsgArray;
????}
????client.sendMsg?
?function(msg){
????????var?chatInfo?
.userName? + " ?:? ?msg;
????????application.chatMsgArray.push(chatInfo);
????????sendMsgToClient(chatInfo);
????}
}

application.onDisconnect?
?function(client)?{
????trace(
用户: client.userName ?离开 );
????var?len?
.userListArray.length;
????
for (var?i 0 ;i < len;i ++ ){
????????
( .userListArray[i]? == ?client.userName){
????????????
.userListArray.splice(i, 1 );
????????????sendUserList();
????????}
????}
}


application.onAppStop?
?function()?{
????delete?
.chatMsg_so;
}

function?checkOnline(userName){
????var?len?
?application.userListArray.length;
????
(application.userListArray[i]? ?userName){
????????????
true ;
????????}
????}
????
false ;
}

function?sendMsgToClient(chatInfo){
????var?len?
?application.clients.length;
????
){
????????application.clients[i].call(
getMsgInfo ,255)">null getUserList ?

this.chatMsgArray = new Array();
this.userListArray = new Array();

这两个定义的数组是在开始定义的,chatMsgArray是存储所有的聊天信息,userListArray是一个存储在线列表的数组


当用户连接进来的时候,我们用了一个函数checkOnline来检查用户是不是在在线列表中,如果在就用this.rejectConnection(client);拒绝这个连接,如果不在就接受这个连接并加到在线列表中

sendUserList函数是向所以客户端发送在线列表信息,application.clients是一个存储所以客户端连接的信息,application.clients.call就是调用客户端函数,使用方法跟客户端调服务器端是一样的.

然后我们增加了1个供客户端调用的函数

client.getMsg = function(){
?return application.chatMsgArray;
}

client.getMsg是返回所有的聊天信息,当用户第一次连接时,得到别人的聊天记录

在用户断开连接的时候增加了将断线用户从在线列表中清除,并且发送在线列表.

再来看看客户端代码:

?

package ?net.smilecn.chat{
????
????
import ?flash.display.Sprite;
????
?flash.net.NetConnection;
????
?flash.net.Responder;
????
?flash.events.NetStatusEvent;
????
?flash.events.SyncEvent;
????
?flash.events.MouseEvent;
????
?fl.controls.TextArea;
??????
?fl.controls.Button;
??????
?fl.controls.TextInput;
????
?fl.controls.Label;
????
?fl.controls.List;
????
?fl.data.DataProvider;

????
????
public class ?Chat? extends ?Sprite{
????????
????????
private ?var?nc:NetConnection;
????????
?var?rtmpUrl:String? rtmp://localhost/chat ;
????????
?var?msg:Label;
????????
?var?userNameInput:TextInput;
????????
?var?enterBtn:Button;
????????
?var?button:Button;
????????
?var?textArea:TextArea;
????????
?var?textInput:TextInput;
????????
?var?userList:List;
????????
????????
?var?userName:String? user002 ;
????
????????
?function?Chat(): void {
????????????userNameInput?
?TextInput();
????????????userNameInput.move(
100 200 );
????????????addChild(userNameInput);
????????????enterBtn?
?Button();
????????????enterBtn.move(
220 );
????????????enterBtn.label?
进入 ;
????????????addChild(enterBtn);
????????????enterBtn.addEventListener(MouseEvent.CLICK,enterBtnClickHandler);
????????????msg?
?Label();
????????????msg.move(
230 );
????????????msg.text?
"" ;
????????????addChild(msg);
????????}
????????
????????
?function?enterBtnClickHandler(event:MouseEvent): {
????????????
(userNameInput.text != ){
????????????????userName?
?userNameInput.text;
????????????????removeChild(userNameInput);
????????????????removeChild(enterBtn);
????????????????removeChild(msg);
????????????
????????????????textArea
?TextArea();
????????????????textArea.setSize?(
300 );
????????????????textArea.move?(
20 );
????????????????addChild?(textArea);

????????????????textInput
?TextInput();
????????????????textInput.width?
140 ;
????????????????textInput.move?(
330 );
????????????????addChild?(textInput);

???????????????????button
?Button();
????????????????button.width
50 ;
???????????????????button.label
发送 ;
???????????????????button.move?(
170 );
????????????????addChild(button);
????????????????button.addEventListener?(MouseEvent.CLICK,sendMsg);
????????????????
????????????????userList?
?List();
????????????????userList.move(
250 );
????????????????userList.setSize(
);
????????????????addChild(userList);
????????????
????????????????nc
?NetConnection();
????????????????nc.addEventListener?(NetStatusEvent.NET_STATUS,netStatusHandler);
????????????????nc.connect?(rtmpUrl,userName);
????????????????nc.client?
;
????????????}
else {
????????????????msg.text?
请输入用户名 ;
????????????}
????????????
????????}
????????
????????
?function?netStatusHandler(event:NetStatusEvent): {
????????????trace(
event.info.code: (event.info.code? NetConnection.Connect.Success ){
????????????????nc.call(
getMsg ?Responder(getMsgResult,getMsgFault))
????????????}
????????}
????????
????????
?function?getMsgResult(re:Array): {
????????????var?s:String
;
????????????var?len:Number?
?re.length;
????????????
){
????????????????s?
+= ?re[i] ;
????????????}
????????????textArea.text?
?s;
????????}
????????
????????
?function?getMsgFault(fe: * ): {
????????????trace(fe);
????????}
????????
????????
?function?sendMsg?(e:MouseEvent): {
????????????nc.call(
sendMsg ;
????????}
????????
????????
?function?getMsgInfo(msg:String): {
????????????textArea.appendText(msg
);
????????}
????????
????????
?function?getUserList(list:Array): {
????????????userList.dataProvider?
?DataProvider(list);
????????}
????}
????
}

客户端首先增加了一个用户输入名字,这是些简单的代码.

然后共享对象的那部份去掉了,加了句这样的代码:nc.client = this;

这句话的意思是指定当前对象为服务器回调方法的对象

public function getMsgInfo(msg:String):void{
?textArea.appendText(msg+"/n");
}
??
public function getUserList(list:Array):void{
?userList.dataProvider = new DataProvider(list);
}

这两个方法就是服务器调用的方法.

在nc连接成功的地方加了句nc.call("getMsg",new Responder(getMsgResult,getMsgFault)),这句是在刚连接成功的时候得到以前用户的聊天记录.

顺便说一句,客户端代码用到了组件,所以要把这些组件放到库中才能运行,上一节也是这样的.


转载:http://blog.csdn.net/arrowyoung/article/details/2448919

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读