尝试使用sleekxmpp连接FCM CCS服务器时出错

时间:2016-06-24 13:21:09

标签: python google-cloud-messaging xmpp firebase-cloud-messaging

我正在尝试使用能够连接Firebase Cloud Messaging服务器的sleekxmpp 1.3.1构建一个非常简单的python3程序。我对以下代码有一个奇怪的Error reading from XML stream问题。我该如何解决?

我们可以从控制台输出中看到启用了STARTTLS。除了禁用from the sleekxmpp source code之外,我找不到其他方法,但它没有解决错误。

根据google documentation发送的第一条消息似乎有效,仍然显示“从xml流中读取错误”。

python代码

FCM_SERVER_URL = "fcm-xmpp.googleapis.com"
FCM_SERVER_PORT = 5236
FCM_SECRET = "XXXXXXX"
FCM_JID = "XXXXXXXX@gcm.googleapis.com"

import logging, socket
from sleekxmpp.clientxmpp import ClientXMPP

class ClientComponent(ClientXMPP):
    def __init__(self):
        ClientXMPP.__init__(self, FCM_JID, FCM_SECRET)
        self.auto_reconnect = False

        server_ip = socket.gethostbyname(FCM_SERVER_URL)
        if self.connect((server_ip, FCM_SERVER_PORT), use_tls = True, reattempt = False):
            self.process(block=True)

logging.basicConfig(level=logging.DEBUG, format='%(levelname)7s %(module)12s:%(lineno)-4s %(message)s')
ClientComponent()

控制台输出

  DEBUG         base:328  Loaded Plugin: RFC 6120: Stream Feature: STARTTLS
  DEBUG         base:328  Loaded Plugin: RFC 6120: Stream Feature: Resource Binding
  DEBUG         base:328  Loaded Plugin: RFC 3920: Stream Feature: Start Session
  DEBUG         base:328  Loaded Plugin: RFC 6121: Stream Feature: Roster Versioning
  DEBUG         base:328  Loaded Plugin: RFC 6121: Stream Feature: Subscription Pre-Approval
  DEBUG         base:328  Loaded Plugin: RFC 6120: Stream Feature: SASL
  DEBUG    xmlstream:546  Connecting to 66.102.1.188:5236
  DEBUG    xmlstream:1162 Event triggered: connected
  DEBUG statemachine:118   ==== TRANSITION disconnected -> connected
  DEBUG    xmlstream:1444 Starting HANDLER THREAD
  DEBUG    xmlstream:1662 Loading event runner
  DEBUG    xmlstream:1309 SEND (IMMED): <stream:stream to="gcm.googleapis.com" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" xml:lang='en' version="1.0">
  ERROR    xmlstream:1492 Error reading from XML stream.
  DEBUG    xmlstream:1162 Event triggered: session_end
  DEBUG    xmlstream:1309 SEND (IMMED): </stream:stream>
  DEBUG    xmlstream:1162 Event triggered: socket_error
WARNING    xmlstream:1342 Failed to send b'</stream:stream>'
  DEBUG    xmlstream:1162 Event triggered: session_end
  DEBUG    xmlstream:1397 Waiting for 3 threads to exit.
  DEBUG    xmlstream:1375 Stopped event runner thread. 2 threads remain.
  DEBUG    xmlstream:1375 Stopped send thread. 1 threads remain.
  DEBUG    scheduler:200  Quitting Scheduler thread
  DEBUG    xmlstream:1375 Stopped scheduler thread. 0 threads remain.
  DEBUG    xmlstream:1162 Event triggered: socket_error
  DEBUG    xmlstream:1162 Event triggered: disconnected
  DEBUG statemachine:118   ==== TRANSITION connected -> disconnected
   INFO    xmlstream:720  Waiting for </stream:stream> from server
  DEBUG    xmlstream:1162 Event triggered: socket_error
  DEBUG    xmlstream:1162 Event triggered: disconnected
  DEBUG statemachine:118   ==== TRANSITION disconnected -> disconnected

2 个答案:

答案 0 :(得分:2)

为了完整起见,这将是一个有效实施的例子:

from sleekxmpp.clientxmpp import ClientXMPP
import logging
import socket
import json
import uuid

logging.basicConfig(level=logging.DEBUG, format="%(levelname)7s %(module)12s:%(lineno)-4s %(message)s")

FCM_SERVER_URL = "fcm-xmpp.googleapis.com"
FCM_SERVER_PORT = 5236
FCM_SERVER_KEY = "XXXXXXXXXXX" # <- Your Server Key
FCM_SENDER_ID = "XXXXXXXXXXX" # <- Your Sender ID
FCM_JID = FCM_SENDER_ID + "@gcm.googleapis.com"
FCM_SERVER_IP = socket.gethostbyname(FCM_SERVER_URL)
TOPIC = "/topics/info"

body = {
    "to": TOPIC, 
    "message_id": uuid.uuid4().hex,
    "data": { "msg": "This is the content"}
}
message = "<message><gcm xmlns='google:mobile:data'>"+json.dumps(body)+"</gcm></message>"

class Client(ClientXMPP):
    def __init__(self):
        ClientXMPP.__init__(self, FCM_JID, FCM_SERVER_KEY, sasl_mech="PLAIN")
        self.add_event_handler("session_start", self.start)
        self.auto_reconnect = False
        self.connect((FCM_SERVER_IP, FCM_SERVER_PORT), use_tls = True, use_ssl = True, reattempt = False)
        self.process(block=True)
    def start(self, event):
        self.send_raw(message)
        self.disconnect(wait=True)


Client()

答案 1 :(得分:0)

我找到了答案on the google list。替换&#34; use_tls = True&#34; by&#34; use_ssl = True&#34;,效果更好。