[Twisted-Python] Python Twisted Questions​​​​

Adam Michalski adam at dormchatapp.com
Thu Dec 18 11:31:18 MST 2014


Hi All!
  
 Thanks so much for the response. We’ve looked into it and here are our findings:
  
  
 We have implemented Linereceiver in our original script to receive the data.
  
 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.
  
 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.
  
 e.g., if the command is:
  
 "demo:{"userid":"1","chatroomid":"1"}"
  
 On server I am getting the below:
  
 demo:{
  
 Please find attached the python script.
  
 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. :)
  
 Again, I really appreciate your help with this. You’ve been super helpful.
  
 -Adam
 Founder, DormChat
 dormchat.com
 
 
Begin forwarded message:

On Tuesday, Dec 2, 2014 at 8:22 PM, Glyph <glyph at twistedmatrix.com>, wrote:


 On Dec 2, 2014, at 23:30, Adam <adam at dormchatapp.com> wrote: 

I assume you're using this tutorial?
 

 <http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server>
 

 It is unfortunate that this has not been updated with better information, it seems to confuse a lot of people making iOS applications ;).
  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)

This is a frequently asked question:
 

 <https://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#Whyisprotocol.dataReceivedcalledwithonlypartofthedataIcalledtransport.writewith>

 
In other words, if you want a message-oriented protocol, you can't just use dataReceived.
 

 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.
 

 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.
 

 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.
 

 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 <http://omz-software.com/pythonista/> contains a template for at least getting the Python runtime going...
 

 -glyph
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://twistedmatrix.com/pipermail/twisted-python/attachments/20141218/bee2460f/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: newdemo.py
Type: application/octet-stream
Size: 9196 bytes
Desc: not available
URL: <http://twistedmatrix.com/pipermail/twisted-python/attachments/20141218/bee2460f/attachment.obj>


More information about the Twisted-Python mailing list