disconnect / apache-websocket

Apache WebSocket module
Apache License 2.0
196 stars 46 forks source link

example module to connect to an arbitrary TCP port #10

Open abligh opened 12 years ago

abligh commented 12 years ago

I am trying to write an example module to connect to an arbitrary TCP port, send the websocket data there, and return data from this port to apache - i.e. send decoded data to the TCP port. This is basically Issue Request #1 save that I was planning to fix it myself.

I am having a little difficulty understanding the threading model this module uses.

Firstly, can I block in my on_message() handler? Can I, for instance, write the data received from the web socket to the TCP port in a manner where the TCP port may block? Or do I have to buffer everything up (possibly infinitely) for another thread to process this, in which case how do I do flow control to stop apache sending me more data?

Secondly, I need to read data back from the tcp port and pass it to server->send(). I cannot solely serve this within my on_message() handler as the on_message() handler may not be called if there is no data going in the other direction (i.e. if there is no data going from websocket to tcp port, I still need to be able to handle data that arrives on the tcp port and needs to go to the web socket). I am presuming I need to create some sort of sister thread for this? If so, is it safe to call server->send() from that thread? Can server->send() itself block? If not, how does apache ever get to tell me to 'stop sending data to me'?

I'm sure these have obvious answers, but being a little new to apache internals I am having difficulty figuring them out.

abligh commented 12 years ago

Well, I did this myself. See http://blog.alex.org.uk/2012/02/16/using-apache-websocket-to-proxy-tcp-connection/

Please feel free to put this in the examples directory. It seems to work.