locked
How to use StreamSocket after calling UpgradeToSslAsync?

    Question

  • I want to login into Facebook XMPP server

    With the tips on http://msdn.microsoft.com/en-us/library/windows/apps/hh780595.aspx ,
    I could create a StreamSocket object and connect to facebook xmpp server.
    I can upgrade the connection to TSL connection, too. (At least the onComplete callback is called.)

    But after upgrading to TSL connection, I send some data and then my callback 'handleAuthResponse' is never called ('onError' either)
    Do I miss something to do? Or do I misunderstand the API usage?

    And after upgrading to TSL connection, the sslStrength of the StreamSocket object is still '0'.
    Is this correct?

    Could someone give me some hints or examples?
    Thanks a lot~

    Here comes part of the code:

        var CHAT_HOST = 'chat.facebook.com';
        var XMPP_PORT = '5222';
        var clientSocket = null;
    
        function createStreamSocketClient() {
            clientSocket = new Windows.Networking.Sockets.StreamSocket();
    
            var serverHostName = new Windows.Networking.HostName(CHAT_HOST);
            clientSocket.connectAsync(serverHostName, XMPP_PORT).then(onClientAccept, onConnectError);
        }
    
        // ....
    
        function upgradeToTlsConnection() {
            // upgrade to TLS connection
            var protectionLevel = Windows.Networking.Sockets.SocketProtectionLevel.sslAllowNullEncryption;
            var serverHostName = new Windows.Networking.HostName(CHAT_HOST);
            clientSocket.upgradeToSslAsync(protectionLevel, serverHostName).then(onTLSAccept, onConnectError);
        }
    
        function onTLSAccept() {
            resultDisplayer.append("Client: TLS connection completed.");
            resultDisplayer.append("Client: socket sslStrength = " + clientSocket.information.sslStrength);
            // ISSUE1: clientSocket.information.sslStrength is still 0 (plainText)
    
            sendAuthInfo();
        }
    
        function sendAuthInfo() {
            var writer = new Windows.Storage.Streams.DataWriter(clientSocket.outputStream);
            writer.writeString(AUTH_XML);
            writer.storeAsync().then(handleAuthResponse, onError);
            writer.detachStream();
        }
    
        function handleAuthResponse() {
            // ISSUE2: this callback and onError are both never called
            var socketInputStream = clientSocket.inputStream;
            var dataReader = new Windows.Storage.Streams.DataReader(socketInputStream);
            
            // ....
        }

    --
    Richard


    Tuesday, March 27, 2012 10:12 AM

All replies

  • Richard,

    I thought that you connected to the Facebook APIs using HTTP ans HTTPS, is that not correct?  Why are you using low level sockets if that is the case?

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, March 27, 2012 2:53 PM
    Moderator
  • Hi Jeff,

    That's because I want to integrate Facebook chat, which asks me to connect via Jabber/XMPP service

    http://developers.facebook.com/docs/chat/

    --

    Richard

    Wednesday, March 28, 2012 1:07 AM
  • Hi Jeff,

    I found that if I set some waiting time between 'upgradeToSslAsync' and its 'then', then the process get work.

    Original Code (part):

        // Original Code
        function upgradeToTlsConnection() {
            // upgrade to TLS connection
            var protectionLevel = Windows.Networking.Sockets.SocketProtectionLevel.sslAllowNullEncryption;
            var serverHostName = new Windows.Networking.HostName(CHAT_HOST);
            clientSocket.upgradeToSslAsync(protectionLevel, serverHostName).then(onTLSAccept, onConnectError);
        }

    Modified Code:

        // Modified Code
        var upgradeToTLSAsyncAction;
    
        function upgradeToTlsConnection() {
            // upgrade to TLS connection
            var protectionLevel = Windows.Networking.Sockets.SocketProtectionLevel.sslAllowNullEncryption;
            var serverHostName = new Windows.Networking.HostName(CHAT_HOST);
            
            // it looks strange, but works
            upgradeToTLSAsyncAction = clientSocket.upgradeToSslAsync(protectionLevel, serverHostName);
            window.setTimeout(chainCallback, 3000);  // do NOT know why but if I didn't wait then I cannot send packets correctly at later steps
        }
    
        function chainCallback() {
            upgradeToTLSAsyncAction.then(onTLSAccept, onConnectError);
        }

    Note that onTLSAccept will be called successfully in both cases. The difference between them is if I didn't wait then I cannot send packet and get response correctly at later steps.

    And in my experiment results, the waiting time is better larger than 1 sec. 

    I cannot understand why. Is it a bug? or do I miss something I should know about this api?

    --

    Richard

    Thursday, March 29, 2012 7:32 AM
  • Could you send me a simple repro of this Richard?

    Send me an email from here and we will connect:  http://blogs.msdn.com/jpsanders/contact.aspx

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, March 29, 2012 11:36 AM
    Moderator
  • Hi Jeff,

    I have sent a message via http://blogs.msdn.com/jpsanders/contact.aspx

    Please check it. Thanks

    --

    Richard

    Friday, March 30, 2012 2:45 AM
  • Could you try one more time?  I did not get a message from you for some reason.

    Jeff Sanders (MSFT)

    Friday, March 30, 2012 12:59 PM
    Moderator
  • Have sent again via http://blogs.msdn.com/jpsanders/contact.aspx

    --

    Richard

    Monday, April 2, 2012 4:07 AM