setting up websockets without command line

别等时光非礼了梦想. 提交于 2019-12-03 04:25:19

问题


How can I follow a tutorial like this without using the command line?

http://www.flynsarmy.com/2012/02/php-websocket-chat-application-2-0/

I need to be able to use websockets but have a shared server and no access to the command line.

I cannot simply run the script from a browser, if the script would stop running when i closed the browser/disconnected from the internet.


回答1:


Assuming your shared hosting provider supports crontab and cron jobs (most of them do), add the following job to the crontab:

@reboot nohup php /path/to/server.php 2>&1 >/dev/null &

Additionally, you need to start it now, so simply create the following PHP file and access it once, in your browser:

<?php shell_exec('nohup php /path/to/server.php 2>&1 > /dev/null &');?>

That should do the trick. Hopefully your shared hosting provider allows execve() calls to be made. Good luck!




回答2:


Well try run Server.php on browser. Most of the time a php-cli-script also works fine on browser (Until using some command-line only functionality like argv/argc etc. ). On other tab of browser on in another browser you can run usual url like screenshot in given tutorial.

One important thing, Please check socket and other required extensions is enabled or not on your shared server.




回答3:


I didn't know about CRON's @reboot directive until just now, so @Octav O solution is probably much better. But while testing, I found the following code (found on Saran's websocket tutorial) quite useful.

Save this as websocket_launch.sh in the same dir as server.php ...

PID=`ps -aef | grep "server.php" | grep -v grep | awk '{print $2}'`
if [ -z $PID ]
then
    #echo "Launching now"
    nohup php server.php > error_log &
else
    #echo "Running as PID $PID"
fi

... then run the shell script with CRON every minute. The script checks if server.php is running, and if not, launches it.



来源:https://stackoverflow.com/questions/14888990/setting-up-websockets-without-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!