<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<span id="mailbox-conversation"><div></div></span><span class="none"><blockquote class="gmail_quote"><span class="none"><blockquote class="gmail_quote"><span class="none"><blockquote class="gmail_quote">Hi All!<br><span id="oldtagunused"><div> <div> </div> <div>Thanks so much for the response. We’ve looked into it and here are our findings:</div> <div> </div> <div> </div> <div>We have implemented Linereceiver in our original script to receive the data.</div> <div> </div> <div>It’s working successfully if we connect to the server through terminal – i.e. we’re getting proper input from terminal client and also proper response is received to the terminal client.</div> <div> </div> <div>It fails when we try to integrate this with the iOS. i.e. it’s connecting to the server successfully but not getting the inputs properly from the iOS client.</div> <div> </div> <div>e.g., if the command is:</div> <div> </div> <div>"demo:{"userid":"1","chatroomid":"1"}"</div> <div> </div> <div>On server I am getting the below:</div> <div> </div> <div>demo:{</div> <div> </div> <div>Please find attached the python script.</div> <div> </div> <div>Note: This seems to be an iOS specific Python issue - when running the same script from Mac OS X it is functioning as expected. If there’s anyone you believe we should redirect the question towards, just let me know. :)</div> <div> </div> <div>Again, I really appreciate your help with this. You’ve been super helpful.</div> <div> </div> <div>-Adam</div> <div>Founder, DormChat</div> <div>dormchat.com</div> </div> <br><span id="orc-full-body-initial-text" style="display:inline;">Begin forwarded message:<br></span><div></div></span><span class="none"><blockquote class="gmail_quote">On Tuesday, Dec 2, 2014 at 8:22 PM, Glyph <<a href="mailto:glyph@twistedmatrix.com">glyph@twistedmatrix.com</a>>, wrote:<br><br><br class=""><div> <blockquote type="cite" class="">On Dec 2, 2014, at 23:30, Adam <<a href="mailto:adam@dormchatapp.com" class="">adam@dormchatapp.com</a>> wrote:</blockquote> <div><br class=""></div>I assume you're using this tutorial?</div> <div><br class=""></div> <div><<a href="http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server" class="">http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server</a>></div> <div><br class=""></div> <div>It is unfortunate that this has not been updated with better information, it seems to confuse a lot of people making iOS applications ;).</div> <div> <blockquote type="cite" class=""><div dir="ltr" class=""><p class="MsoNormal"><span style="color:rgb(30,63,16)" class="">1. Please find attached Python script. We have integrated the Twisted framework with iOS for chat module. Attached is our Python demo code in which we have implemented this. The issue is when we try to connect with the server through the terminal, we are getting the complete correct response without any error. But when it was integrated on the iOS app, data is getting received in chunks (i.e. if data exceeds 1366 bytes or above in json size it comes in pieces which creates a problem when sending and receiving chats)</span></p></div></blockquote>This is a frequently asked question:</div> <div><br class=""></div> <div><<a href="https://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#Whyisprotocol.dataReceivedcalledwithonlypartofthedataIcalledtransport.writewith" class="">https://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#Whyisprotocol.dataReceivedcalledwithonlypartofthedataIcalledtransport.writewith</a>><br class="">
</div> <br class=""><div class="">In other words, if you want a message-oriented protocol, you can't just use dataReceived.</div> <div class=""><br class=""></div> <div class="">For sending short JSON structures back and forth, line-delimited data structures are a reasonable choice.  On the Twisted side, you can use twisted.protocols.basic.LineReceiver, and override lineReceived instead of dataReceived.</div> <div class=""><br class=""></div> <div class="">On the iOS side, you need a radically different structure for buffering your input; you can't just stuff it into a buffer on the stack.  One quick hacky way to do this would be to give your object a NSString bufferString instance variable, and do bufferString = [bufferString stringByAppendingString: [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]]; NSArray* lines = [bufferString componentsSeparatedByString: @"\r\n"];, bufferString = [lines lastObject];, then loop over the other items in lines.</div> <div class=""><br class=""></div> <div class="">Keep in mind you also need to do this for output; as you send output to the server, you may not be able to send all of it at once, so you need to keep a buffer, and keep track of how much you've written, etc.</div> <div class=""><br class=""></div> <div class="">Or you could just build Python into your app on iOS and run Twisted in there.  If you manage to get that working be sure to let us know how it goes :-).  I am lead to believe that <<a href="http://omz-software.com/pythonista/" class="">http://omz-software.com/pythonista/</a>> contains a template for at least getting the Python runtime going...</div> <div class=""><br class=""></div> <div class="">-glyph</div>
</blockquote></span>
</blockquote></span></blockquote></span></blockquote></span>
</body></html>