dscizpq790832708 2016-03-02 17:27
浏览 64

PHP Ratchet无法从其他设备连接

I'm starting using PhP Ratchet socket. Following the guides I could made a simple chat application and it's working within the same computer. Example, if I open up chrome and firefox, I can interact send and receive messages, ok.

The problem is when I try to use the chat app from another computer, but within the same internet connection, or even when I tried to test it online.

When on lan, the other computers can't connect with the socket and online no one can connect.

By looking around, I found about using '0.0.0.0' or even port 5555 to enable connections from anyone. But even when using this, I can't connect.

How can I solve this? This is my files:

server.php

<?php

    use Ratchet\Server\IoServer;
    use Ratchet\http\HttpServer;
    use Ratchet\WebSocket\WsServer;

    require __DIR__ . '/../vendor/autoload.php';

    $server = IoServer::factory(
        new HttpServer(
            new WsServer(
                new Chat()
            )
        ), 2000, '0.0.0.0'
    );

    $server->run();

?>

And client js file:

var socket = new WebSocket('ws://127.0.0.1:2000');
  • 写回答

1条回答 默认 最新

  • douxi3432 2016-03-02 17:53
    关注

    127.0.0.1 will always point to the local device, which, on devices other than the socket server, will not point to the device that is hosting the socket server. If the device that is running the socket server is not routeable from the internet, you will not be able to connect to the socket from the internet.

    You can test from other devices on your LAN by figuring out what your network IP is for the device that is running the server. Then in your client code, connect to that IP. It will probably be something close to 192.168.x.x, e.g. 192.168.1.12 (it could also be in the 10.x.x.x or 172.16.x.x address spaces). Then simply use that address to connect from your client script for testing:

    var socket = new WebSocket('ws://192.168.1.12:2000');
    

    You may still run into trouble if, for whatever reason, your network is configured to drop packets on port 2000. If so, it should be fairly easy for you to change to a different port for both your server and client.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?