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
1 Answer
Reset to default 3Try 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
版权声明:本文标题:javascript - Ratchet socket from Android and IOS clients - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744739068a2622503.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论