简体   繁体   English

如何(本地)从PHP调用JavaScript函数(在Raspberry Pi上)

[英]How to (locally) call a JavaScript function from PHP (on Raspberry Pi)

I'm in a slight predicament where I can't really understand how to proceed. 我处于一种轻微的困境,我无法真正理解如何继续。 I've got a set-up where I'm hosting a website locally, on a Raspberry Pi which is used to send commands to a LED-donut + speaker. 我有一个设置,我在本地托管一个网站,在Raspberry Pi上,用于向LED甜甜圈+扬声器发送命令。 The website also contains a scheduler, which allows people to drag/drop music playlists and licht-scenes to a date and time. 该网站还包含一个调度程序,允许人们将音乐播放列表和licht-scenes拖放到日期和时间。 The concept is that these will be fired off automatically. 这个概念是这些将自动触发。

The interface on this website has several functions we can call. 本网站的界面有几个我们可以调用的功能。 The point is, I'm using Cronjobs to fire off PHP scripts that will for instance grab information out of my MySQL database and use that information (date+time+string of licht-scene / audio file) to send to the LED-donut and speakers. 关键是,我正在使用Cronjobs来启动PHP脚本,例如从我的MySQL数据库中获取信息并使用该信息(日期+时间+ licht-scene / audio文件的字符串)发送到LED-donut和发言者。

My issue here is that everywhere I've searched, people tell me that PHP can't access JS functions, simply because they are server-side vs client-side or that it has to be within one file. 我的问题在于, 无论哪里搜索,人们都告诉我PHP无法访问JS功能,仅仅因为它们是服务器端与客户端,或者它必须在一个文件中。

However, all of these scripts etc. are run locally on a Raspberry Pi and I'm working on pre-existing code. 但是,所有这些脚本等都是在Raspberry Pi上本地运行的,而我正在处理预先存在的代码。

I'll show you some snippets: 我会告诉你一些片段:

A function I would like to call: (URL_address is not very useful, same with startmodulo. It can be applied afterwards.) 我想调用的函数:(URL_address不是很有用,与startmodulo相同。之后可以应用它。)

function sendStartSceneAjax(URL_address, filename, startmodulo){
$.ajax({
    type: "POST",
    dataType: "json",
    url: "/cgi-bin/scene?"+Math.random(), //"/cgi-bin/scene?"
    data: {file: filename, startdelaymodulo:startmodulo },
    success: function (data) {
        updateScene(data);
        console.log(data);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        document.getElementById("sceneplay").innerHTML = "error playing: " + filename;
        console.error("OH SHIT STARTSCENEAJAX " + errorThrown + " " + XMLHttpRequest + URL_address + filename + startmodulo);
    } }); }

Just a makeshift test I've built -- obviously doesn't work, but might show you what I am thinking of: 只是我已经建立的一个临时测试 - 显然不起作用,但可能会告诉你我在想什么:

if($split_sentence_2 == $nowDate)
{
        ?>
        <button name="Play" onclick="startSelectedSceneFromDB('Blauwe_Pulse.csv');" class="play" title="<? echo $label_play; ?>"></button>
        <?   
}

I'm really hoping you guys and gals could at least give me some pointers on how to proceed. 我真的希望你们和gals至少可以给我一些关于如何进行的指示。 It would be highly appreciated! 非常感谢!

ps I wanted to post more links to show that I have been doing research, but I'm only allowed 2 at the moment. ps我想发布更多链接来表明我一直在做研究,但我现在只允许2。 My apologies! 我很抱歉!

What you are requesting is not possible using standard AJAX. 您使用标准AJAX无法提出要求。 The client can request anything from the server, but the server cannot directly communicate to the client. 客户端可以从服务器请求任何内容,但服务器无法直接与客户端通信。 Previous solutions to this have been rapid polling or long polling, which are starting to be replaced with the technology that I would recommend you investigating, WebSockets. 以前的解决方案是快速轮询或长轮询,这些都开始被我建议您调查的技术WebSockets取代。

There are abundances of information on WebSockets on the internet and a few google searches should start putting you down the right path, but the basics are that WebSockets will allow two-way direct communication between the server and the client in near real-time. 互联网上有大量关于WebSockets的信息,一些谷歌搜索应该开始让你走上正确的道路,但基本原则是WebSockets将允许服务器和客户端之间的近乎实时的双向直接通信。 This would then allow your server to tell clients to do something, essentially allowing your server to call javascript in the browser. 这将允许您的服务器告诉客户端做某事,基本上允许您的服务器在浏览器中调用javascript。

Update: After re-reading your application I realize that you might be talking about a slightly different scenario, of which web sockets would still work, but if you don't actually need the web page at all, and just want this to be an automated background task, then perhaps moving all code to the server-side would be what you need. 更新:重新阅读您的应用程序后,我意识到您可能正在谈论一个稍微不同的场景,其中Web套接字仍然可以工作,但如果您根本不需要网页,只是希望这是一个自动后台任务,然后将所有代码移动到服务器端将是您所需要的。 However, if you do need a web-page to drive all of this, then WebSockets will be your best bet. 但是,如果您确实需要一个网页来驱动所有这些,那么WebSockets将是您最好的选择。

There are many implementations out there, and I would recommend doing your own research to find the one that works best for you, but the one that I have used before is autobahn.js which follows the WAMP (Web-Application Messaging Protocol) specification. 有很多实现,我建议你自己做研究找到最适合你的那个,但我以前用过的那个是遵循WAMP(Web应用程序消息传递协议)规范的autobahn.js There should be a PHP WAMP compliant implementation somewhere that you can use for the server. 在某个地方应该有一个PHP WAMP兼容的实现,您可以将其用于服务器。

Start script: 启动脚本:

function sendStartSceneAjax(URL_address, filename, startmodulo){
$.ajax({
    type: "POST",
    dataType: "json",
    url: "/cgi-bin/scene?"+Math.random(), //"/cgi-bin/scene?"
    data: {file: filename, startdelaymodulo:startmodulo },
    success: function (data) {
        updateScene(data);
        console.log(data);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        document.getElementById("sceneplay").innerHTML = "error playing: " + filename;
        console.error("OH SHIT STARTSCENEAJAX " + errorThrown + " " + XMLHttpRequest + URL_address + filename + startmodulo);
    } }); }

And: 和:

 setInterval( function( e ) {
            $.post( '/to_php_script_to_get_song_info.php', function( returnData ) {
                    if ( returnData != '' ) {
                        returnData = JSON && JSON.parse(json) || $.parseJSON(json);
                        sendStartSceneAjax( returnData.URL_address, returnData.filename, returnData.startmodulo );
                    }
                });
        }, 500 );

Then on the php part: 然后在PHP部分:

//find whatever songinfo is about to start
<?php
    $data = array(
        'URL_address' => $found_url_address,
        'filename' => $found_filename,
        'startmodulo' => $found_startmodulo,
);

echo json_encode( $data );
?>

This way you call the bit that searches songinfo and lighting stuff on every 500 ms, and return data back if found some. 通过这种方式,您可以每隔500毫秒调用一次搜索songinfo和照明内容的位,如果找到则返回数据。 When the script found some it calls the sendStartSceneAjax with the appropriate variables. 当脚本找到一些时,它会使用适当的变量调用sendStartSceneAjax。 Maybe you can use some of it. 也许你可以使用其中一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM