vspr
vspr

Reputation: 39

Python Twisted. Implement 'socket.io'-like events

Can't figure out how to implement custom events inside a Protocol

E.g. server-side

client.on('checkin', function (name) { ... });

and client-side

socket.emit('checkin',name);

My current thought is to make if condition depending on a data received in dataReceived(self, data) function. A kind of header stored in data.

Any suggestion or docs would be appreciated. Thanks.

Upvotes: 0

Views: 171

Answers (3)

Glyph
Glyph

Reputation: 31860

When you say "custom events" here, you're talking about implementing a wire protocol. The "events" you're talking about are messages within that protocol, not things that you can do on any arbitrary socket. I don't know what protocol socket.io speaks. If you're writing the client (and it's not a web browser) then you might want to use AMP, which will give you an extensible way to do client/server communication.

Upvotes: 2

SingleNegationElimination
SingleNegationElimination

Reputation: 156138

you may not want to use a bare protocol for this,at least not with TCP. dataReceived is called whenever the socket has data, with no natural divisions between "chunks" of data sent. an easier approach is to use one of the classes that wraps Protocol, such as LineReceiver, which implements dataReceived and calls lineReceived ones its gathered a whole line of data.

Upvotes: 1

Jan Matejka
Jan Matejka

Reputation: 1970

Why don't you look on other protocols how they do it?

Eg. IRCClient would be good example I guess - http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.2.0/twisted/words/protocols/irc.py#L1707

Upvotes: 1

Related Questions