dongqu9917 2014-02-06 01:17
浏览 42

PHP WebSocket客户端? 或者在PHP内部运行JavaScript(没有浏览器)? 希望与Ratchet

I'm very new to WebSockets and have been playing with Ratchet and gotten a simple chat room to work on my AWS EC2 instance. Now I'm trying to use another service which can run a custom PHP script (hosted on their servers) to open a websocket connection on my server and send a message.

I got the EC2 URL working, and when I run it in my browser (including on my phone, which isn't on the same network as my computer) it works. I'm simply passing a testing API key and the value in the URL for now (grabbed with PHP GET), like so:

http://ec2-22-222-22-222.us-west-2.compute.amazonaws.com/path/to/script/ws-trigger.php?key=12345&name=Bing

This works, but only because the ws-trigger.php page loads the following JavaScript and runs it from the browser:

<script type="text/javascript">
    var conn = new WebSocket('ws://22.222.22.222:8080');
    conn.onopen = function(e) {
        console.log("Connection established!");
        conn.send(<?php echo $_GET['name']; ?>);
    };
</script>

What is the easiest way for me to run this "connect to the websocket" and "send a message over the socket" via PHP? If I have to use a library I'd prefer use Ratchet since I already am, but I feel like I must be missing something because this cannot be a unique problem.

I have tried searching StackOverflow already, but have had issues with the few answers I'd already found (like phpwebsocket was giving me troubles before I moved to Ratchet, and this one isn't a PHP WS client at all)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog
    • ¥15 Excel发现不可读取的内容
    • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题