javascript - Ratchet socket from Android and IOS clients - Stack Overflow

I've written socket PHP code using Ratchet. Here is simple code, that works when I send and get

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://

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744739069a4590921.html

相关推荐

  • javascript - Ratchet socket from Android and IOS clients - Stack Overflow

    I've written socket PHP code using Ratchet. Here is simple code, that works when I send and get

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信