javascript - 来自 Android 和 IOS 客户端的 Ratchet 套接字

标签 javascript android ios sockets ratchet

我使用 Ratchet 编写了套接字 PHP 代码.这是简单的代码,当我从网络发送和接收消息时可以使用 - javascript:

    use Ratchet\MessageComponentInterface;

    class Chat implements MessageComponentInterface{
        protected $clients;

        public function __construct(){
            $this->clients = new SplObjectStorage();
        }

        function onOpen(\Ratchet\ConnectionInterface $conn)
        {
            echo "Did Open\n";
            $this->clients->attach($conn);
        }


        function onClose(\Ratchet\ConnectionInterface $conn)
        {
            echo "Did Close\n";
            $this->clients->detach($conn);
        }


        function onError(\Ratchet\ConnectionInterface $conn, \Exception $e)
        {
            echo "The following error occured: ". $e->getMessage();
        }


        function onMessage(\Ratchet\ConnectionInterface $from, $msg)
        {
            $msgObj = json_decode($msg);
            echo $msgObj->msg;
            foreach($this->clients as $client){
                if($client !== $from){
                    $client->send($msg);
                }
            }
        } 
}

问题是当我使用来自 Android 应用程序的 java 客户端时。我使用 Activity 中的线程。它没有异常(exception),没有错误。 client.isConnected() 为真。但没有服务器代码未被调用 - onOpen 方法、onMessage 和其他方法。我怎样才能解决这个问题。 IOS 的情况几乎相同。客户端连接到服务器,但没有调用这些 Ratchet 方法。它们只能从 javascript 调用。 Java 代码:

new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    client = new Socket("XX.XX.XX.XX", 2000);

                    printWriter = new PrintWriter(client.getOutputStream());
                    printWriter.write("Android Message");
                    printWriter.flush();
                    printWriter.close();
                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

最佳答案

尝试用于
安卓:https://github.com/TooTallNate/Java-WebSocket
iOS:https://github.com/square/SocketRocket
因为 Ratchet 是 WebSocket。你的主机名应该从 ws://开始

关于javascript - 来自 Android 和 IOS 客户端的 Ratchet 套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31566556/

相关文章:

javascript - Web 组件中 shadow dom 的外部样式表

android - Firebase ANR - 甚至不使用 firebase

java - EditText .setError() 未在重新创建时应用我的自定义错误图标

android - 在 Android 中关闭奖励视频广告

ios - ViewController 不符合协议(protocol) AnyObject 错误与 UISwipeGestureRecognizer

ios - 为什么我无法在 Swift 4 中使用 `svg` 将 `SVGKit` 数据转换为 UIImage?

javascript - 替换jquery中 anchor 标签的值

javascript - 导航网格寻路(多边形)

ios - 如何确定 UITableView 性能低下的原因

javascript - this.props 不是 componentDidUpdate() 中的函数