admin管理员组

文章数量:1391924

I've written socket PHP code using Ratchet . Here is simple code, that works when I send and get messages from web - 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);
                }
            }
        } 
}

The problem is when I use java client - from Android App. I use thread from Activity. It has no exceptions, no errors. client.isConnected() is true. But no server code is not called - onOpen method, onMessage and others. How Can I fix this. Almost the same situation is with IOS. Client Connects to server but nither of those Ratchet methods are called. They are called only from javascript. Java code :

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();

I've written socket PHP code using Ratchet . Here is simple code, that works when I send and get messages from web - 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);
                }
            }
        } 
}

The problem is when I use java client - from Android App. I use thread from Activity. It has no exceptions, no errors. client.isConnected() is true. But no server code is not called - onOpen method, onMessage and others. How Can I fix this. Almost the same situation is with IOS. Client Connects to server but nither of those Ratchet methods are called. They are called only from javascript. Java code :

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();
Share Improve this question asked Jul 22, 2015 at 14:47 Misha AkopovMisha Akopov 13.1k27 gold badges71 silver badges86 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

Try to use for
Android: https://github./TooTallNate/Java-WebSocket
iOS: https://github./square/SocketRocket
Because Ratchet is WebSocket. And your host name should starts from ws://

本文标签: javascriptRatchet socket from Android and IOS clientsStack Overflow