Celeste :: 深蓝世纪 :: 技术专区

Welcome to you visit Celesteteam CeTech!

« 企业建站的网站布局、效果设计问题--深圳深蓝世纪The Delegate Class 代理类的使用 »

Maintaining a chat log in FlashCom through amfphp

Integrating Remoting and FlashCom (or FMS2) can be quite powerful, yet the integration is so poorly documented that it's no wonder most people don't even try to do it.

I was asked by a client to implement a chat log for a SharedObject-based Instant messenger-like app. At that point I was a bit sceptic about the feasibility of doing that since I was using SharedObjects and not calling a method on the NetConnection object. Turns out that if you connect to the SharedObject on the serverside you can spy on the changes to it, so this is indeed possible without major rearchitecting.

Now as for actually saving the data there are a few options. Jesse suggested making a master SharedObject that contained only one slot that would compile activity across all the other SharedObjects and that could be retrieved using another Flash movie. Clever, but that wasn't going to do it for this case since the traffic for the site means we're expecting about 1-10 MB of data per day so that would quickly accumulate to the point where the Flash movie would take a whole bunch of time to load, plus displaying and searching it would be a pain. So a log file was in order. Of course this is possible now using the new 'File' object in FMS2, however that would mean saving on our Windows-based FlashCom server and the rest of the site is hosted entirely on an unrelated Linux box, and our only access was through VNC. Of course, File can't do things like save to a mysql database or email you the log, which the chosen solution can.

So I used FlashCom's Remoting and amfphp to do the job. First off, the FlashCom code:

load("netservices.asc");

NetServices.setDefaultGatewayUrl("http://localhost/amfphp/gateway.php");
var nsc = NetServices.createGatewayConnection();

var chatlogService = nsc.getService("ChatLogService", new ChatLogService());
var clInt;
var chatLog = "";
var chatSo;

function syncChatSo(list)
{
        for(var i = 0; i < list.length; i++)
        {
                //Log the messages
                if(list[i].code == 'change')
                {
                        chatLog += '[' + (new Date()).toString() + '] ' + chatSo[list[i].name].message + "\n";
                }
        }
}

function logChat()
{
        trace('logChat');
        chatlogService.logChat(chatLog);
        chatLog = "";
}

application.onAppStart = function()
{
        trace("application started");
        clearInterval(clInt);
        clInt = setInterval(logChat, 60000);
       
        chatSo = SharedObject.get('chatSo', true);
        chatSo = syncChatSo;
}

function ChatLogService()
{
       
}

ChatLogService.prototype.logChat_Result = function(result)
{
        trace("logChat_Result");
        trace(result);
}

Here the key points are:

  • Include netservices.asc file
  • Create a gateway, and get the default connection
  • Get the service, create an instance of the responder 'ChatLogService'
  • onAppStart, connect to the shared object
  • Set up an interval that calls the logChat function every so often
  • on SharedObject sync, append to the chat log
  • Every so often, call chatLogService.logChat and empty the buffer
  • Get the results of the call back in logChat_Result

Note that you don't have to create a new 'ChatLogService' class for every remote method you want to create. If you wanted to call for example getLogSize on the chat log service, you would simply need to declare a function getLogSize_Result on the ChatLogService prototype.

The interval scheme might be a bit over the top but it simply stops the server from being called everytime someone sends a chat message; instead of calling the server 50 times a minutes with 50 bytes of data, call it once with 2.5kb, reducing useless server load.

Note that my actual implementation is a bit more convoluted than this one as I have one SharedObject per user instead of one for the whole chat, but the principle is basically the same, just more SharedObjects to watch.

Now as for the amfphp service:

<?php
class ChatLogService
{
        function ChatLogService()
        {
                include("ChatLogService.methodTable.php");
        }
       
        /**
        * Appends stuff to the chat log
        * @access remote
        */

        function logChat($log)
        {
                return file_put_contents('chatlog.log', $log, FILE_APPEND);
        }
}
?>

As you see there's nothing special about the amfphp service, it is really straightforward. The returned value is the number of bytes written to the log file. Note that with an extra line of code or two you could have it save the data to a mysql database instead of a flat file. For that purpose, you could further split the $log variable; instead of a plain string, you could send an array of objects, each containing keys like date, from, to, and message. Finally, you could secure the method using roles and _authenticate. Note that NetDebug::trace will not work with FlashCom, so testing your service in the Service browser is highly recommended.

copy from: http://www.5etdemi.com/blog/archives/2006/03/maintaining-a-chat-log-in-flashcom-through-amfphp/

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新留言

最近发表

Powered By Z-Blog 1.6 Final Build 60816

关于我们 | 服务项目 | 客户案例 | 联系我们 :: Copyright @ Celesteteam.com