javascript - 如何完全实现消息轮询的服务器发送事件

标签 javascript php jquery polling server-sent-events

我正在尝试使用 API 与服务器通信。

为了简化操作,我编写了一个脚本,该脚本将使用 php 脚本与 API 通信。

我的目标是每秒向 API 查询一次,以查看他们的队列中是否有新消息。

有人建议我使用 server-sent events 方法,让服务器仅在有新内容时才向客户端发送响应。

这是我的php脚本

<?php

    // Register autoloader function
    spl_autoload_register('apiAutoloader');
    header("Content-Type: text/event-stream\n\n");

    $config = array('host' => 'server:IP',              //REQUIRED - Server Name
                    'port' => 8018,                     //REQUIRED - Server Port
                    'userID' => 'user',                 //REQUIRED - UserID to login with
                    'password' => '123456',             //REQUIRED - password for the UserID to login with
                    );

    try {

        //create a new instance of icws
        $icws = new API\ICWS($config); 

        //create a new session
        $icws->createSession(true);     

        while(1){

            $result = $icws->processMessage();
            echo json_encode($result);  

            ob_flush();
            flush();
            sleep(1);

        }

    } catch(Exception $e){
        echo $e->getMessage();
    }

    function apiAutoloader($className)
    {
        $className = ltrim($className, '\\');
        $fileName  = '';
        $namespace = '';

        if ($lastNsPos = strripos($className, '\\')) 
        {
            $namespace = substr($className, 0, $lastNsPos);
            $className = substr($className, $lastNsPos + 1);
            $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
        }

        $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

        $toLoad = dirname(__FILE__).'/'.$fileName;

        if (is_file($toLoad) === true)
        {
            include $toLoad;
        }
    }
?>

当用户访问我的页面时,这是我运行的代码

<script>

    $(function(){

        function isset(a, b){

            if(typeof a !== "undefined" && a){
                return a
            }

            return b;
        }


        var evtSource = new EventSource("poll.php");

        evtSource.onmessage = function(e) {

            $.each(e.data.calls, function(i, item){

                $.each(item, function(z, c){

                    var interactionId = isset(c.interactionId, 0);
                    var Eic_CallDirection = isset(c.Eic_CallDirection, '');
                    var Eic_State = isset(c.Eic_State, '');
                    var AccoRDI_mid = isset(c.AccoRDI_mid, '');
                    var Eic_RemoteAddress = isset(c.Eic_RemoteAddress, '');

                    if(Eic_State == '' || Eic_CallDirection == ''){
                        return;
                    }

                    //incoming call that is not answered
                    if( Eic_CallDirection == 'I' && Eic_State == 'A'){
                        console.log('Incomming Call From ' + Eic_RemoteAddress + ' MID: ' + AccoRDI_mid );
                        return;
                    }

                    //on hold
                    if( Eic_State == 'H'){
                        console.log('Phone number ' + Eic_RemoteAddress + ' is on hold' );
                        return;
                    }

                    //voicemail
                    if( Eic_State == 'M'){
                        console.log('Phone number ' + Eic_RemoteAddress + ' is on leaving a voicemail' );
                        return;
                    }

                    //connected call
                    if(Eic_State == 'C'){
                        console.log('Live Call With ' + Eic_RemoteAddress );
                        return;
                    }

                    //Dialling call
                    if(Eic_State == 'O'){
                        console.log('Dialling ' + Eic_RemoteAddress );
                        return;
                    }

                    //Dialling call
                    if(Eic_State == 'R'){
                        console.log('Outbound call is rining and waiting for answer ' );
                        return;
                    }

                    //Call Disconnected
                    if(Eic_State == 'I' || Eic_State == 'E' ){
                        console.log('Hungup with ' + Eic_RemoteAddress );
                        return;
                    }

                });
            });
        }
    });
</script>

我上面的代码不工作。

我的控制台中没有任何内容。但是这个错误 Firefox 无法在/icws/poll.php 建立到服务器的连接

我怎样才能让它工作? 我用PHP脚本实现的方式对吗?

最佳答案

浏览器请求事件源 php 文件的状态代码是什么?尝试使用 Firebug 来确保它不是 404。

还有关于 php 代码。

根据您在问题中包含的 Mozilla 来源。 来自服务器的正确响应应命名并以换行符分隔

header("Content-Type: text/event-stream\n\n");     
while (1) {     
  echo "event: ping\n"; //event name
  echo 'data: '.json(); //event data
  Echo "\n\n"; //required  

  ob_flush(); flush(); sleep(1);    
 }

事件示例

event: userconnect
data: {"username": "bobby", "time": "02:33:48"} 

event: usermessage
data: {"username": "bobby", "time": "02:34:11", "text": "Hi everyone."} 

关于javascript - 如何完全实现消息轮询的服务器发送事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30279887/

相关文章:

javascript - 如何在不丢失滚动位置的情况下刷新代码?

javascript - 如何替换字符串中的反斜杠双引号?

php - MySQL+PHP : How to paginate data from complex query with ORDER BY on user-selected column

jquery - 使用 JQuery 构建选项卡

javascript - 让用户下载经过处理的 SVG

javascript - 操作 MongoDB 中引用数组中元素的顺序

javascript - localStorage 对象的 getter 和 setter 方法的目的是什么?

php - 限制 shell_exec 的执行时间

php - 无法加入 php/mysql 中的第三个表来获取用户名

php - 剑道网格点击新的更新按钮没有努力