【发布时间】:2015-10-12 12:44:03
【问题描述】:
我已经使用 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);
}
}
}
}
问题是当我使用 java 客户端时 - 从 Android 应用程序。我使用来自 Activity 的线程。它没有例外,没有错误。 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();
【问题讨论】:
标签: javascript android ios sockets ratchet