|
1 ############################################################################### |
|
2 # # |
|
3 # # Copyright (C) 2011-2014 Tavendo GmbH |
|
4 # # |
|
5 # # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # # you may not use this file except in compliance with the License. |
|
7 # # You may obtain a copy of the License at |
|
8 # # |
|
9 # # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # # |
|
11 # # Unless required by applicable law or agreed to in writing, software |
|
12 # # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # # See the License for the specific language governing permissions and |
|
15 # # limitations under the License. |
|
16 # # |
|
17 ############################################################################### |
|
18 |
|
19 import sys |
|
20 import csv |
|
21 import time |
|
22 |
|
23 from twisted.python import log |
|
24 from twisted.internet import reactor |
|
25 import argparse |
|
26 |
|
27 |
|
28 from autobahn.twisted.websocket import WebSocketClientProtocol, WebSocketClientFactory |
|
29 |
|
30 class MyClientProtocol(WebSocketClientProtocol): |
|
31 |
|
32 #def __init__(self, rows): |
|
33 |
|
34 def onConnect(self, response): |
|
35 print("Server connected: {0}".format(response.peer)) |
|
36 reactor.callLater(0, self.send_messages) |
|
37 |
|
38 def onOpen(self): |
|
39 print("WebSocket connection open.") |
|
40 |
|
41 def send_messages(self): |
|
42 with open("annotsroll_sample_formated.txt", 'rU') as datafile: |
|
43 for row in list(datafile): |
|
44 self.sendMessage(row, isBinary = False) |
|
45 time.sleep(0.1) |
|
46 |
|
47 #time.sleep(0.1) |
|
48 #self.sendMessage('{"user":"Julien"}'.encode('utf8')) |
|
49 print("Goodbye") |
|
50 self.factory.reactor.callLater(0.1, reactor.stop) |
|
51 |
|
52 def onClose(self, wasClean, code, reason): |
|
53 print("WebSocket connection closed: {0}".format(reason)) |
|
54 |
|
55 |
|
56 |
|
57 if __name__ == '__main__': |
|
58 |
|
59 log.startLogging(sys.stdout) |
|
60 |
|
61 parser = argparse.ArgumentParser(description='Simulate an (osc) pianoroll client.') |
|
62 #parser.add_argument('datafile', metavar='DATAFILE', help='The file containing the pianoroll data (CSV).') |
|
63 parser.add_argument('-e', '--event', dest='event', metavar='EVENT', required=True, help='the event code.') |
|
64 |
|
65 args = parser.parse_args() |
|
66 |
|
67 #with open(args.datafile, 'rU') as datafile: |
|
68 # reader = csv.reader(datafile, delimiter=' ') |
|
69 |
|
70 factory = WebSocketClientFactory("ws://127.0.0.1:8090/annot?event=%s" % args.event, debug=True) |
|
71 factory.protocol = MyClientProtocol |
|
72 |
|
73 reactor.connectTCP("127.0.0.1", 8090, factory) |
|
74 reactor.run() |
|
75 |