views:

63

answers:

2

We are building an iPhone chat application.

When sending from the browser to the iPhone a JSON chat message:

    {"content":"Hi"}

The iPhone receives:

    {"content":{"0":72,"1":105,"length":2}}

But, we intend for it to receive the same exact message.

To reproduce this issue, first install node.js & redis. Then:

  • Get the code:

    git clone git://github.com/acani/acani.git
    cd acani
    git submodule update --init
    
  • Start Redis on the default port.

  • From http://github.com/acani/acani-node:

    node acani-node-server.js # run node.js chat server
    # open index.html in a Google Chrome or Firefox and follow instructions.
    
  • Open Lovers.xcodeproj located in http://github.com/acani/acani-chat/tree/master/Lovers2/, and change LoversAppDelegate.m to initially load the ChatViewController instead of the HomeViewController.

    homeViewController = [[HomeViewController alloc] init]; # comment out this line
    # change the next line to:
    navigationController = [[UINavigationController alloc] initWithRootViewController:[[ChatViewController alloc] init]];
    # Then, build & run.
    

Thanks!

Matt

+1  A: 

My guess is you need to escape the string (JSON) being sent, using stringByAddingPercentEscapesUsingEncoding and then unescape it on receipt.

The first three numbers are 072 - in decimal that's 'H'. Which makes me think a " might be getting lost due to transmission without encoding. There are other things against this theory but I think it is worth looking at.

Adam Eberbach
That makes sense. Thanks. I'll try that tonight and report back. I guess I figured the AsyncSocket library would take care of that...
MattDiPasquale
+3  A: 

We figured it out. It wasn't the iPhone or Objective-C at all. The conversion error was happening on the node.js server. We forgot to put quotes around the string values of the JSON object, and so the JSON.stringify() JavaScript function was converting the strings as shown above... except we were doing something like: {"content":Hi}. When we changed it to: {"content":"Hi"}, it worked fine. Duhh...

MattDiPasquale