javascript - Ratchet socket from Android and IOS clients -
i've written socket php code using ratchet . here simple code, works when send , messages 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 $client){ if($client !== $from){ $client->send($msg); } } } }
the problem when use java client - android app. use thread activity. has no exceptions, no errors. client.isconnected() true. no server code not called - onopen method, onmessage , others. how can fix this. same situation ios. client connects server nither of ratchet methods called. called 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();
try use
android: https://github.com/tootallnate/java-websocket
ios: https://github.com/square/socketrocket
because ratchet websocket. , host name should starts ws://
Comments
Post a Comment