public function sendMessage msg this- checkConnection if this- s- send

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public function sendMessage($msg) {
$this->checkConnection();
if ($this->s->send('<message id=""><gcm xmlns="google:mobile:data">'.htmlspecialchars(json_encode($msg), ENT_QUOTES).'</gcm></message>')) {
while ($data = $this->s->recv()) {
if ($data->name != 'MESSAGE')
continue;
if ($data->attr('type') == 'error') {
$text = $data->findByName('text');
if ($text) {
throw new XMPP\GCM\Exception("Stanza error: ".$text[0]->value);
} else
throw new XMPP\GCM\Exception("Unknown stanza error: ".$data);
}
$gcm_data = $data->findByNS("google:mobile:data");
if ($gcm_data) {
$json = json_decode($gcm_data[0]->value, true);
if (!$json || !isset($json['message_type']))
throw new XMPP\GCM\Exception("Json parse error: ".$gcm_data[0]->value);
if ($json['message_type'] == 'ack') {
// ok, успешно отправлено
return $this;
} elseif ($json['message_type'] == 'control') {
$this->google_close_connection = true;
continue;
} else {
throw new XMPP\GCM\Exception("GCM error (".$json['error']."): ".$json['error_description']);
}
} else
throw new XMPP\GCM\Exception("GCM data not found: ".$data);
}
}
return $this->sendMessage($msg);
}