我正在尝试使用 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/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/