> On Dec 2, 2014, at 23:30, Adam <a...@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
 
<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
 
<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
_______________________________________________
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

Reply via email to