1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
def run_threaded():
from ws4py.client.threadedclient import WebSocketClient
class EchoClient(WebSocketClient):
def opened(self):
self.send("hello")
def closed(self, code, reason=None):
print(("Closed down", code, reason))
def received_message(self, m):
print(m)
self.close()
try:
ws = EchoClient('wss://localhost:9000/ws')
ws.connect()
ws.run_forever()
except KeyboardInterrupt:
ws.close()
def run_tornado():
from tornado import ioloop
from ws4py.client.tornadoclient import TornadoWebSocketClient
class MyClient(TornadoWebSocketClient):
def opened(self):
self.send("hello")
def closed(self, code, reason=None):
print(("Closed down", code, reason))
ioloop.IOLoop.instance().stop()
def received_message(self, m):
print(m)
self.close()
ws = MyClient('wss://localhost:9000/ws')
ws.connect()
ioloop.IOLoop.instance().start()
def run_gevent():
from gevent import monkey; monkey.patch_all()
import gevent
from ws4py.client.geventclient import WebSocketClient
ws = WebSocketClient('wss://localhost:9000/ws')
ws.connect()
ws.send("hello")
def incoming():
while True:
m = ws.receive()
if m is not None:
print(m)
else:
break
ws.close()
gevent.joinall([gevent.spawn(incoming)])
#run_gevent()
run_threaded()
run_tornado()
|