In AMF3, my socket server encodes a Python dictionary but the second message is shortened

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

In AMF3, my socket server encodes a Python dictionary but the second message is shortened

Ethan Kennerly
Hi,

Because I enjoy programming in Python and animating in Flash, I enjoy PyAMF.
I needed to push data from Python server to Flash client.
So I switched from NetConnection to Socket, following the PyAMF Twisted
socket tutorial.
I guess that tutorial is sending a string, so is not encoding an object.
For my game, I need to send a small, extensible message.
I adapted it to encode a Python dictionary (:: ActionScript Object).
AMF0 socket protocol passes a very basic test,
but when I switch the object encoding from AMF0 to AMF3,
the server amputates part of the second message (I think it is the
beginning).
Here is the relevant code for sending:

    def __init__(self):
        self.encoder = pyamf.get_encoder(object_encoding)

    def send(self, message):
        logging.debug('embassy.send: ' + message.__repr__())
        self.encoder.writeElement(message)
        self.transport.write(
                self.encoder.stream.getvalue())
        self.encoder.stream.truncate()

Other than encoding, I believe my server and client accords with the
Twisted socket tutorial.
Again, it works in AMF0, and sends first message in AMF3, but amputates
part of the following messages.
Also, if I send a string instead of a Python dictionary, the message is
not amputated in AMF3.
And of course the original tutorial works (using strings?).
This shortened message is received similarly by the text client and
Flash client,
so I suspect my mistake is in the above server code.

AMF0 is fine for me, but I spent hours today wondering what was going on,
I thought I would see if someone could point out my mistake.

Thank you for reading,

-- Ethan
_______________________________________________
PyAMF users mailing list - [hidden email]
http://lists.pyamf.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: In AMF3, my socket server encodes a Python dictionary but the second message is shortened

Nick Joyce
On 27 Feb 2010, at 16:55, Ethan Kennerly wrote:

> Hi,
>
> Because I enjoy programming in Python and animating in Flash, I enjoy PyAMF.
> I needed to push data from Python server to Flash client.
> So I switched from NetConnection to Socket, following the PyAMF Twisted socket tutorial.
> I guess that tutorial is sending a string, so is not encoding an object. For my game, I need to send a small, extensible message. I adapted it to encode a Python dictionary (:: ActionScript Object).
> AMF0 socket protocol passes a very basic test,
> but when I switch the object encoding from AMF0 to AMF3,
> the server amputates part of the second message (I think it is the beginning).
> Here is the relevant code for sending:
>
>   def __init__(self):
>       self.encoder = pyamf.get_encoder(object_encoding)
>
>   def send(self, message):
>       logging.debug('embassy.send: ' + message.__repr__())
>       self.encoder.writeElement(message)
>       self.transport.write(
>               self.encoder.stream.getvalue())
>       self.encoder.stream.truncate()
>
> Other than encoding, I believe my server and client accords with the Twisted socket tutorial. Again, it works in AMF0, and sends first message in AMF3, but amputates part of the following messages.
> Also, if I send a string instead of a Python dictionary, the message is not amputated in AMF3.
> And of course the original tutorial works (using strings?).
> This shortened message is received similarly by the text client and Flash client,
> so I suspect my mistake is in the above server code.
>
> AMF0 is fine for me, but I spent hours today wondering what was going on,
> I thought I would see if someone could point out my mistake.
>
> Thank you for reading,
>
> -- Ethan

Make sure that you are clearing the encoders context each time you dispatch a message to the transport. Flash clears the context between each 'context' in this case per message.

The code would look like:

  def __init__(self):
      self.encoder = pyamf.get_encoder(object_encoding)
      self.stream = self.encoder.stream
      self.context = self.encoder.context

  def send(self, message):
      logging.debug('embassy.send: ' + message.__repr__())
      self.encoder.writeElement(message)
      self.transport.write(self.stream.getvalue())
      self.stream.truncate()
      self.context.clear()

Hope that helps.

Nick
_______________________________________________
PyAMF users mailing list - [hidden email]
http://lists.pyamf.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: In AMF3, my socket server encodes a Python dictionary but the second message is shortened

Ethan Kennerly
Nick,

After your modification, in both AMF0 and AMF3,
with Python socket client and Flash socket client,
the server delivers the second message!

Thank you!

-- Ethan

Nick Joyce wrote:

>
> Make sure that you are clearing the encoders context each time you dispatch a message to the transport. Flash clears the context between each 'context' in this case per message.
>
> The code would look like:
>
>   def __init__(self):
>       self.encoder = pyamf.get_encoder(object_encoding)
>       self.stream = self.encoder.stream
>       self.context = self.encoder.context
>
>   def send(self, message):
>       logging.debug('embassy.send: ' + message.__repr__())
>       self.encoder.writeElement(message)
>       self.transport.write(self.stream.getvalue())
>       self.stream.truncate()
>       self.context.clear()
>
> Hope that helps.
>
> Nick
> _______________________________________________
> PyAMF users mailing list - [hidden email]
> http://lists.pyamf.org/mailman/listinfo/users
>
>
>  

_______________________________________________
PyAMF users mailing list - [hidden email]
http://lists.pyamf.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: In AMF3, my socket server encodes a Python dictionary but the second message is shortened

Thijs Triemstra
Hi Ethan,

would be good to update the Socket tutorial with objects, do you have time to patch the Socket example in the trunk? If you do, please put the source in a new ticket on http://dev.pyamf.org.

Thanks,

Thijs

On 28 Feb 2010, at 19:49, Ethan Kennerly wrote:

> Nick,
>
> After your modification, in both AMF0 and AMF3,
> with Python socket client and Flash socket client,
> the server delivers the second message!
>
> Thank you!
>
> -- Ethan
>
> Nick Joyce wrote:
>>
>> Make sure that you are clearing the encoders context each time you dispatch a message to the transport. Flash clears the context between each 'context' in this case per message.
>>
>> The code would look like:
>>
>>  def __init__(self):
>>      self.encoder = pyamf.get_encoder(object_encoding)
>>      self.stream = self.encoder.stream
>>      self.context = self.encoder.context
>>
>>  def send(self, message):
>>      logging.debug('embassy.send: ' + message.__repr__())
>>      self.encoder.writeElement(message)
>>      self.transport.write(self.stream.getvalue())
>>      self.stream.truncate()
>>      self.context.clear()
>>
>> Hope that helps.
>>
>> Nick
>> _______________________________________________
>> PyAMF users mailing list - [hidden email]
>> http://lists.pyamf.org/mailman/listinfo/users
>>
>>
>>  
>
> _______________________________________________
> PyAMF users mailing list - [hidden email]
> http://lists.pyamf.org/mailman/listinfo/users


_______________________________________________
PyAMF users mailing list - [hidden email]
http://lists.pyamf.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: In AMF3, my socket server encodes a Python dictionary but the second message is shortened

Ethan Kennerly
This weekend, I think I would.

-- Ethan

Thijs Triemstra | Collab wrote:
> Hi Ethan,
>
> would be good to update the Socket tutorial with objects, do you have time to patch the Socket example in the trunk? If you do, please put the source in a new ticket on http://dev.pyamf.org.
>
> Thanks,
>
> Thijs
>
>  

_______________________________________________
PyAMF users mailing list - [hidden email]
http://lists.pyamf.org/mailman/listinfo/users