summaryrefslogtreecommitdiff
path: root/README
blob: ea5b165b6c3f958ab6e472bfadff4b3979eab823 (plain)
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
66
67
68
69
70
71
=================
websocket-client
=================

websocket-client module  is WebSocket client for python. This provide the low level APIs for WebSocket. All APIs are the synchronous functions.

License
============

 - LGPL

Installation
=============

This module is tested on only Python 2.7.

Type "python setup.py install" or "pip install websocket-client" to install.

This module does not depend on any other module.

Example
============

Low Level API example::

    from websocket import create_connection
    ws = create_connection("ws://localhost:5000/echo")
    print "Sending 'Hello, World'..."
    ws.send("Hello, World")
    print "Sent"
    print "Reeiving..."
    result =  ws.recv()
    print "Received '%s'" % result
    ws.close()


JavaScript websocket-like API example::

  import websocket
  import thread
  import time
  
  def on_message(ws, message):
      print message
  
  def on_error(ws, error):
      print error
  
  def on_close(ws):
      print "### closed ###"
  
  def on_open(ws):
      def run(*args):
          for i in range(3):
              time.sleep(1)
              ws.send("Hello %d" % i)
          time.sleep(1)
          ws.close()
          print "thread terminating..."
      thread.start_new_thread(run, ())
  
  
  if __name__ == "__main__":
      websocket.enableTrace(True)
      ws = websocket.WebSocketApp("ws://localhost:5000/chat",
                                  on_message = on_message,
                                  on_error = on_error,
                                  on_close = on_close)
      ws.on_open = on_open
      
      ws.run_forever()