[RED5]red5服务器端与客户端实例聊天室源代码
ED5]red5服务器端与客户端实例,[RED5]red5服务器端与客户端实例聊天室源代码
ED5]red5服务器端与客户端实例
- import org.red5.server.adapter.ApplicationAdapter;
- import org.red5.server.api.IConnection;
- import org.red5.server.api.IScope;
- import org.red5.server.api.Red5;
- import org.red5.server.api.service.IServiceCapableConnection;
- import org.slf4j.Logger;
- import org.red5.logging.Red5LoggerFactory;
- import java.util.*;
- public class Application extends ApplicationAdapter
- {
- private static Logger log = Red5LoggerFactory.getLogger(Application.class, "echat");
- /**
- 15 * 每个新的客户端来连接的时候调用!
- 16 * 这里我们覆盖了父类的实现。
- 17 */
- public boolean appConnect(IConnection con, Object[] params)
- {
- log.debug("new client connectting chat room");
- return true;
- }
- /**
- * 当客户端断开连接的时候调用!
- * 这里我们覆盖了父类的实现。
- */
- public void appDisconnect(IConnection conn)
- {
- log.debug(conn.getClient().getId() + " disconnect");
- }
- /**
- * 加入聊天室,必须带上用户名,假如用户名为空,则不能发送消息,也不能收到消息。
- * @param params 客户端调用服务器端的参数。
- */
- public void jionChatRoom(Object[] params)
- {
- String nickName = params[0].toString();
- if (null == nickName || "".equals(nickName))
- {
- return ;
- }
- else
- {
- // CuPlayer.com提示:设置用户昵称。
- IConnection conn = Red5.getConnectionLocal();
- conn.setAttribute("nickName", nickName);
- }
- // 发通知给聊天室的所有人,有新人加入了。
- IScope scope = Red5.getConnectionLocal().getScope();
- Iterator it = scope.getConnections().iterator();
- int x = 0;
- for (;it.hasNext();)
- {
- Set connections = (Set)it.next();
- IConnection tempConn = (IConnection)connections.iterator().next();
- if (tempConn instanceof IServiceCapableConnection)
- {
- IServiceCapableConnection sc = (IServiceCapableConnection) tempConn;
- // CuPlayer.com提示:服务器端调用客户端flash方法。
- sc.invoke("showJoinInInfo", new Object[]{nickName});
- }
- }
- }
- /**
- * 给聊天室的所有人发送消息
- * @param params
- */
- public void sayToAll(Object[] params)
- {
- IConnection conn = Red5.getConnectionLocal();
- //conn.setAttribute(”nickName”, nickName);
- String nickName = conn.getAttribute("nickName").toString();
- String sayWhat = params[0].toString();
- // CuPlayer.com提示:发消息给聊天室的所有人.
- IScope scope = Red5.getConnectionLocal().getScope();
- Iterator it = scope.getConnections().iterator();
- for (;it.hasNext();)
- {
- Set connections = (Set)it.next();
- IConnection tempConn = (IConnection)connections.iterator().next();
- if (tempConn instanceof IServiceCapableConnection)
- {
- IServiceCapableConnection sc = (IServiceCapableConnection) tempConn;
- // CuPlayer.com提示:服务器端调用客户端flash方法。
- sc.invoke("showMessage", new Object[]{nickName+ " : " +sayWhat});
- }
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application fontSize="12" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
- <mx:Script>
- <![CDATA[
- private var nc:NetConnection;
- private function connectAndJoinRoom(e:Event):void
- {
- var nickName:String = nickNameTextInput.text;
- if (nickName == "")
- {
- return;
- }
- else
- {
- if (nc == null)
- {
- initialNetConnection();
- }
- }
- }
- private function initialNetConnection():void
- {
- nc = new NetConnection();
- nc.addEventListener(NetStatusEvent.NET_STATUS, connectStatus);
- nc.client = this;
- nc.connect("rtmp://localhost/echat", null);
- }
- private function connectStatus(event:NetStatusEvent) : void
- {
- if (event.info.code == "NetConnection.Connect.Success")
- {
- nc.call("jionChatRoom", null, nickNameTextInput.text);
- sendButton.enabled = true;
- }
- }
- private function sendMessage():void
- {
- var sendString:String = inputWhatYouWantSay.text;
- inputWhatYouWantSay.text = "";
- nc.call("sayToAll", null, sendString);
- }
- public function showJoinInInfo(message:String) : void
- {
- roomArea.text += message + " 加入聊天室" + "/n";
- }
- public function showMessage(message:String) : void
- {
- roomArea.text += message + "/n";
- }
- ]]>
- </mx:Script>
- <mx:VBox x="20" y="20">
- <mx:HBox>
- <mx:Label text="昵称 : "></mx:Label>
- <mx:TextInput id="nickNameTextInput">
- </mx:TextInput>
- <mx:Button click="connectAndJoinRoom(event)" label="加入聊天室">
- </mx:Button>
- </mx:HBox>
- <mx:HBox width="100%" height="300">
- <mx:TextArea height="100%" width="100%" id="roomArea">
- </mx:TextArea>
- </mx:HBox>
- <mx:HBox width="100%">
- <mx:TextInput width="100%" id="inputWhatYouWantSay">
- </mx:TextInput>
- <mx:Button enabled="false" id="sendButton" label="发送" click="sendMessage()">
- </mx:Button>
- </mx:HBox>
- </mx:VBox>
- </mx:Application>
热门文章推荐
- [Red5]Red5之Flash流媒体服务器的安装与使用教程完整版(组图)
- [RED5]搭建RED5直播用流媒体服务(搭直播环境)
- [RED5]Red5的直播与点播的压力测试(并发数的测试)
- [RED5]red5流媒体服务器(开源免费)的安装方法
- [RED5]用red5做视频直播(red5流媒体直播)
- [RED5]Red5在线录制音视频源代码示例
- [RED5]Red5 Client 连接 Red5Server java代码
- [RED5]red5流媒体播放平台搭建环境方法
请稍候...