Feeds:
文章
留言

Archive for 十月 28th, 2008

Read Full Post »

最近發現用 C# 撰寫的 FluorineFx 專案,這個專案特別的地方是支援 RTMP Protocol, AMF0, AMF3,更棒的是有提供 RTMP Client API。雖然 Red5 也有提供類似的 API 不過實作尚未完成。至於 FluorineFx 的 RTMP Client 測試之後也發現沒有我想像中的好,目前只支援 NetConnection, SharedObject,但是獨缺我最需要要的 NetStream….

只用 NetConnection+SharedObject 可讓 .NET 透過 Flash Remoting 與遠端的 RTMP Server 溝通。以下使用 Red5 附得 SOSample 這個簡單聊天程式來測試 FluorineFx。測試環境如下:

1. Windows XP SP2
2. Red5 0.7
3. FluorineFx 1.0.0.13
4. VS2005

程式碼(修改 FluorineFx 內附的 SharedObjectTest 範例)

using System;
using FluorineFx;
using FluorineFx.Net;

namespace SharedObjectTest
{
    class Program
    {
        NetConnection _netConnection;
        RemoteSharedObject _sharedObject;

        static void Main(string[] args)
        {
            Program program = new Program();
            program.Connect();

            System.Console.WriteLine("Connecting to server...");
            string cmd;
            do{
                cmd = System.Console.ReadLine();
                if (cmd == "exit") {
                    break;
                }

                program.send(cmd);
            }while(true);
        }

        public void Connect()
        {
            // Create NetConnection client
            _netConnection = new NetConnection();
            _netConnection.OnConnect += new ConnectHandler(_netConnection_OnConnect);
            _netConnection.OnDisconnect += new DisconnectHandler(_netConnection_OnDisconnect);
            _netConnection.NetStatus += new NetStatusHandler(_netConnection_NetStatus);
            
            _netConnection.Connect("rtmp://localhost:1935/SOSample");
        }

        void _netConnection_OnConnect(object sender, EventArgs e)
        {
            System.Console.WriteLine("Connected to server. Connecting to RSO...");
            //_sharedObject = RemoteSharedObject.GetRemote("users", _netConnection.Uri.ToString(), false);
            //Our custom UsersRSO will handle "ChatMsg" messages
            _sharedObject = RemoteSharedObject.GetRemote("SampleChat" /*typeof(UsersRSO), "users"*/, _netConnection.Uri.ToString(), false);
            
            _sharedObject.OnConnect += new ConnectHandler(_sharedObject_OnConnect);
            _sharedObject.OnDisconnect += new DisconnectHandler(_sharedObject_OnDisconnect);
            _sharedObject.NetStatus += new NetStatusHandler(_sharedObject_NetStatus);
            _sharedObject.Sync += new SyncHandler(_sharedObject_Sync);
            _sharedObject.Connect(_netConnection);
        }

        void _netConnection_OnDisconnect(object sender, EventArgs e)
        {
            System.Console.WriteLine("Connection disconnected.");
        }

        void _netConnection_NetStatus(object sender, NetStatusEventArgs e)
        {
            string level = e.Info["level"] as string;
            if (level == "error")
            {
                //received an error
                System.Console.WriteLine("Error: " + e.Info["code"] as string);
                System.Console.WriteLine("Client not connected. Press 'Enter' to exit");
            }
            if (level == "status")
            {
                System.Console.WriteLine("Status: " + e.Info["code"] as string);
            }
        }

        void _sharedObject_OnConnect(object sender, EventArgs e)
        {
            System.Console.WriteLine("Connected to RSO.");
        }

        void _sharedObject_OnDisconnect(object sender, EventArgs e)
        {
            System.Console.WriteLine("Disconnected RSO.");
        }

        void _sharedObject_Sync(object sender, SyncEventArgs e)
        {
            
            ASObject[] changeList = e.ChangeList;
            for (int i = 0; i < changeList.Length; i++)
            {
                ASObject info = changeList[i];

                if (info["name"] != null && info["code"] == "change") {
                    String d = _sharedObject.GetAttribute("SampleChat").ToString();
                    Console.WriteLine("Receive Data: " + d);
                } else
                    System.Console.WriteLine(info["code"].ToString());
            }

        }

        public void send(String data) {
            _sharedObject.SetProperty("SampleChat", data);
        }

        void _sharedObject_NetStatus(object sender, NetStatusEventArgs e)
        {
            string level = e.Info["level"] as string;
            if (level == "error")
            {
                //received an error
                //System.Console.WriteLine("Error: " + e.Info["code"] as string);
            }
            if (level == "status")
            {
                System.Console.WriteLine("Status: " + e.Info["code"] as string);
            }
        }
    }
}

image

Read Full Post »