websocket Client-side support

435 views
Skip to first unread message

NIM4N

unread,
Dec 25, 2013, 1:57:37 AM12/25/13
to python-...@googlegroups.com
hi I want to use tornado websocket client and I see this document:
http://www.tornadoweb.org/en/stable/websocket.html#client-side-support
but I can't findout how to use It .
I know how to write It by twisted but I want to know how to hendle It by tornado?

this is my twisted code:

from twisted.internet import reactor
from autobahn.websocket import WebSocketClientFactory, WebSocketClientProtocol, connectWS


class EchoClientProtocol(WebSocketClientProtocol):

   def sendHello(self):
      self.sendMessage("Hello, world!")

   def onOpen(self):
      self.sendHello()

   def onMessage(self, msg, binary):
      print "Got echo: " + msg
      reactor.callLater(1, self.sendHello)


if __name__ == '__main__':

   factory = WebSocketClientFactory("ws://localhost:8000/cart/status")
   factory.protocol = EchoClientProtocol
   connectWS(factory)
   reactor.run()

A. Jesse Jiryu Davis

unread,
Dec 25, 2013, 3:04:22 PM12/25/13
to python-...@googlegroups.com
Clone Tornado's repository from GitHub and start the websocket chat demo. cd into its directory and do:

PYTHONPATH=. python demos/websocket/chatdemo.py

Point your browser at http://localhost:8888 and make sure the demo is working. Now, try running this script:

import json
from tornado.ioloop import IOLoop
from tornado import gen
from tornado.websocket import websocket_connect

@gen.coroutine
def main():
    client = yield websocket_connect('ws://localhost:8888/chatsocket')
    client.write_message(json.dumps({'body': 'foo'}))
    message = yield client.read_message()
    print message

if __name__ == "__main__":
    IOLoop.current().run_sync(main)

The script makes a WebSocketClientConnection, writes a message to the server, then reads the server's response.


--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Ali E.İMREK

unread,
Dec 26, 2013, 2:06:29 AM12/26/13
to python-...@googlegroups.com
I've simple example at http://armuting.blogspot.com/2013/08/tornado-websocket-client.html that show you the way.



2013/12/25 NIM4N <freem...@googlemail.com>

--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Ali E.İMREK

NIM4N

unread,
Dec 26, 2013, 2:38:04 AM12/26/13
to python-...@googlegroups.com
tnks alot
Reply all
Reply to author
Forward
0 new messages