棘轮插座从Android和IOS客户端
Ratchet socket from Android and IOS clients
我使用Ratchet编写了套接字PHP代码。下面是简单的代码,当我从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);
}
}
}
}
问题是当我使用java客户端-从Android应用程序。我使用线程从活动。它没有异常,没有错误。client.isConnected()为真。但是没有服务器代码不调用- onOpen方法,onMessage等。我怎样才能解决这个问题。IOS也是如此。客户端连接到服务器,但没有调用这两个棘轮方法。它们只能从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();
尝试使用
Android: https://github.com/TooTallNate/Java-WebSocket
iOS: https://github.com/square/SocketRocket
因为棘轮是WebSocket。你的主机名应该以ws://
相关文章:
- 客户端服务器REST API captcha实现
- 使用谷歌应用程序脚本将服务器端数据表返回到客户端
- 如何使用Socket.io将命令从客户端发送到服务器
- 如何轻松地将服务器端变量从Java代码转移到客户端代码
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- 从客户端获取修改后的对象,并将其与服务器上的原始对象组合
- 如何将我的javascript库公开给其他客户端使用
- 是否可以使用JavaScript/AAJAX在客户端创建一个文件
- 如何使用Javascript客户端对象模型检索Sharepoint 2010列表项权限
- 返回/从Twit's客户端.get
- 将客户端特定的日期格式返回到服务器MVC4
- MobileFirst:在客户端运行计时器作业-最佳选项
- 标签客户端的设置值
- 我的客户端选项是什么
- 任何适用于iOS的WebRTC客户端实现
- 如何使用Javascript或在客户端获取设备(iOS,Android)CPU和内存
- 棘轮插座从Android和IOS客户端
- Selenium WebDriverJs无法使用iOS创建新客户端
- 在iOS上,我如何在web应用的客户端调整一个非常大的图像的大小
- 使用套接字.io与iOS通过套接字.Swift客户端