fritzy / SleekXMPP

Python 2.6+/3.1+ XMPP Library
http://groups.google.com/group/sleekxmpp-discussion
Other
1.1k stars 299 forks source link

Trying to connect the FCM CCS server with sleekxmpp #417

Closed azmeuk closed 8 years ago

azmeuk commented 8 years ago

I am trying to build a very simple python3 program using sleekxmpp 1.3.1 that would be able to connect the Firebase Cloud Messaging server. I have a strange Error reading from XML stream issue with the following code. How can I fix it?

We can see from the console output that STARTTLS is enabled. I found no other way than disable it from the sleekxmpp source code, but it does not solved the bug.

The first message sent seems valid according to the google documentation, still the "error reading from xml stream" appears.

The python code

    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()

The console output

  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
sourc7 commented 8 years ago

I have tried to connect FCM server using SleekXMPP, it has an issue at connecting FCM server using TLS protocol, you may need to change the TLS to SSL, as the code below.

FCM_SERVER_URL = "fcm-xmpp.googleapis.com"
FCM_SERVER_PORT = 5235
FCM_SECRET = "SERVER-KEY"
FCM_JID = "SENDER-ID@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_ssl = True, reattempt = False):
            self.process(block=False)

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)7s %(module)12s:%(lineno)-4s %(message)s')
ClientComponent()
azmeuk commented 8 years ago

use_ssl is the option I needed! Thanks