doumeng9188 2013-11-09 14:36
浏览 57
已采纳

在没有服务器根访问权的情况下构建聊天脚本的有效方法是什么

I have a hosing account. (cPanel or DirectAdmin) So I don't have root access, i can't use exec() or shell_exec() functions (Blocked by Server Admin).

I know best way for chat is socket programing but it needs Terminal Commands like:

PHP ./server.php

But i don't have access to the Terminal.

I did too many searches and found some methods but I'm not sure for performance because they are using a Javascript command setTimeout() for connecting to the chat database or file.

I think if i used setTimeout('refresh()',1000) there is a better way instead of connecting to the chat database every 1 sec. my method:

refresh=function()
{
//there is flag in session, true means there is new message from sender (session_set_save_handler)
//check flag value in the session, if its true then refresh chat database
}
setTimeout('refresh()',1000)

I used a flag because i dont want to connect to the database every 1 sec, maybe there is no new messages from sender, but we're forcing the server to refresh every 1 sec.

Is my method better or not ? is there a better way for build a chat script without Server Root access?

Thanks alot. Sorry for bad English

  • 写回答

3条回答 默认 最新

  • duanqiongchong0354 2013-11-09 14:58
    关注

    That solution will definitely work, using setTimeout to check for new messages every second or so. There are other technologies such as comet although these are not possible in PHP, as stated in the question.

    Here is an example that uses PHP and stores chat history in an SQL database, the ajax function to get new chat messages:

    //Updates the chat
    function updateChat(){
      $.ajax({
    
        type: "GET",
        url: "update.php",
        data: {  
            'state': state,
            'file' : file
            },
        dataType: "json",
        cache: false,
        success: function(data) {
    
            if (data.text != null) {
                for (var i = 0; i < data.text.length; i++) {  
                $('#chat-area').append($("<p>"+ data.text[i] +"</p>"));
            }
    
            document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight;
    
        }  
    
        instanse = false;
        state = data.state;
        setTimeout('updateChat()', 1);
    
        },
      });
    }
    

    As you can see the last line uses setTimeout to call the function every 1 second.

    Messages are sent separately by a different function:

    //send the message
    function sendChat(message, nickname) {       
      $.ajax({
           type: "POST",
           url: "process.php",
           data: {  
                    'function': 'send',
                    'message': message,
                    'nickname': nickname,
                    'file': file
                    },
           dataType: "json",
           success: function(data){
    
           },
        });
    }
    

    As I mentioned in my comment above, there are some advantages to using server technologies other than PHP. Most PHP solutions use a database to persist chat messages between requests to the server, this creates a lot more server overhead than is really needed, a node.js solution can instead store the messages in an array that stays in memory within the server, and in addition use sockets see here for an example.

    Edit - to cache an sql query in memory

    If you are using MySQL it is possible to prepend your query with a comment to imply that the query should be cached in memory, something like:

    $res   = $mysqli->query("/*" . MYSQLND_QC_ENABLE_SWITCH . "*/" . "SELECT message FROM chatroom WHERE id = $roomId");
    

    For more information see example 1, here. The example times the queries and could be useful as a benchmark on your Server.

    Caching can occur on the server even if the code does not explicitly ask for it, after all the amount of memory required to store the contents of a chatroom is very small - it's only a few lines of text! The example I took the Javascript code from, above, stores the text in a file, which would almost certainly be stored in memory on the Web Server. If the db server is running on the same host as the web site then the request may well not result in any disk activity, making the overhead also very small. The webserver / db connection may also be in memory rather than sockets.

    The best solution will very much depend on your server setup, best to get something working, then optimise it. I do know from experience that the node.js solution is very fast.

    Edit - Answer to flag question

    Setting a flag in the client Javascript would not work, as other clients could submit messages without the flag for the current client being reset. Setting a flag in the PHP on the server is tricky, persistent variables that are not specific to clients (from sessions or cookies), and not stored in databases have to be saved in files: PHP: persistent variable value. The static keyword is not quite the same as it would be in C or similar language. This is one of the main advantage of using Node.js, persistent arrays are very easy to create.

    One solution would be to save the messages as json in a file, append each new message as it is received, and return the whole file to the user, once a second. The file could be restricted to 100 lines or so, this would ensure that it stays stored in cache memory somewhere (ramdisk, OS disk cache, or at worse hardware cache on the harddisk itself).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳