34 from __future__
import print_function
38 from socket
import error
39 from twisted.internet
import reactor
40 from rosbridge_server
import RosbridgeUdpSocket,RosbridgeUdpFactory
49 from std_msgs.msg
import Int32
54 if __name__ ==
"__main__":
55 rospy.init_node(
"rosbridge_websocket")
56 rospy.on_shutdown(shutdown_hook)
62 RosbridgeUdpSocket.fragment_timeout = rospy.get_param(
'~fragment_timeout',
63 RosbridgeUdpSocket.fragment_timeout)
64 RosbridgeUdpSocket.delay_between_messages = rospy.get_param(
'~delay_between_messages',
65 RosbridgeUdpSocket.delay_between_messages)
66 RosbridgeUdpSocket.max_message_size = rospy.get_param(
'~max_message_size',
67 RosbridgeUdpSocket.max_message_size)
68 RosbridgeUdpSocket.unregister_timeout = rospy.get_param(
'~unregister_timeout',
69 RosbridgeUdpSocket.unregister_timeout)
70 if RosbridgeUdpSocket.max_message_size ==
"None":
71 RosbridgeUdpSocket.max_message_size =
None 74 RosbridgeUdpSocket.topics_glob = [
75 element.strip().strip(
"'")
76 for element
in rospy.get_param(
'~topics_glob',
'')[1:-1].split(
',')
77 if len(element.strip().strip(
"'")) > 0]
78 RosbridgeUdpSocket.services_glob = [
79 element.strip().strip(
"'")
80 for element
in rospy.get_param(
'~services_glob',
'')[1:-1].split(
',')
81 if len(element.strip().strip(
"'")) > 0]
82 RosbridgeUdpSocket.params_glob = [
83 element.strip().strip(
"'")
84 for element
in rospy.get_param(
'~params_glob',
'')[1:-1].split(
',')
85 if len(element.strip().strip(
"'")) > 0]
88 RosbridgeUdpSocket.authenticate = rospy.get_param(
'~authenticate',
False)
89 port = rospy.get_param(
'~port', 9090)
90 interface = rospy.get_param(
'~interface',
"")
92 RosbridgeUdpSocket.client_count_pub = rospy.Publisher(
'client_count', Int32, queue_size=10, latch=
True)
93 RosbridgeUdpSocket.client_count_pub.publish(0)
95 if "--port" in sys.argv:
96 idx = sys.argv.index(
"--port")+1
97 if idx < len(sys.argv):
98 port = int(sys.argv[idx])
100 print(
"--port argument provided without a value.")
103 if "--interface" in sys.argv:
104 idx = sys.argv.index(
"--interface")+1
105 if idx < len(sys.argv):
106 interface = int(sys.argv[idx])
108 print(
"--interface argument provided without a value.")
111 if "--fragment_timeout" in sys.argv:
112 idx = sys.argv.index(
"--fragment_timeout") + 1
113 if idx < len(sys.argv):
114 RosbridgeUdpSocket.fragment_timeout = int(sys.argv[idx])
116 print(
"--fragment_timeout argument provided without a value.")
119 if "--delay_between_messages" in sys.argv:
120 idx = sys.argv.index(
"--delay_between_messages") + 1
121 if idx < len(sys.argv):
122 RosbridgeUdpSocket.delay_between_messages = float(sys.argv[idx])
124 print(
"--delay_between_messages argument provided without a value.")
127 if "--max_message_size" in sys.argv:
128 idx = sys.argv.index(
"--max_message_size") + 1
129 if idx < len(sys.argv):
130 value = sys.argv[idx]
132 RosbridgeUdpSocket.max_message_size =
None 134 RosbridgeUdpSocket.max_message_size = int(value)
136 print(
"--max_message_size argument provided without a value. (can be None or <Integer>)")
139 if "--unregister_timeout" in sys.argv:
140 idx = sys.argv.index(
"--unregister_timeout") + 1
141 if idx < len(sys.argv):
142 unregister_timeout = float(sys.argv[idx])
144 print(
"--unregister_timeout argument provided without a value.")
147 if "--topics_glob" in sys.argv:
148 idx = sys.argv.index(
"--topics_glob") + 1
149 if idx < len(sys.argv):
150 value = sys.argv[idx]
152 RosbridgeUdpSocket.topics_glob = []
154 RosbridgeUdpSocket.topics_glob = [element.strip().strip(
"'")
for element
in value[1:-1].split(
',')]
156 print(
"--topics_glob argument provided without a value. (can be None or a list)")
159 if "--services_glob" in sys.argv:
160 idx = sys.argv.index(
"--services_glob") + 1
161 if idx < len(sys.argv):
162 value = sys.argv[idx]
164 RosbridgeUdpSocket.services_glob = []
166 RosbridgeUdpSocket.services_glob = [element.strip().strip(
"'")
for element
in value[1:-1].split(
',')]
168 print(
"--services_glob argument provided without a value. (can be None or a list)")
171 if "--params_glob" in sys.argv:
172 idx = sys.argv.index(
"--params_glob") + 1
173 if idx < len(sys.argv):
174 value = sys.argv[idx]
176 RosbridgeUdpSocket.params_glob = []
178 RosbridgeUdpSocket.params_glob = [element.strip().strip(
"'")
for element
in value[1:-1].split(
',')]
180 print(
"--params_glob argument provided without a value. (can be None or a list)")
184 if RosbridgeUdpSocket.services_glob:
185 RosbridgeUdpSocket.services_glob.append(
"/rosapi/*")
187 Subscribe.topics_glob = RosbridgeUdpSocket.topics_glob
188 Advertise.topics_glob = RosbridgeUdpSocket.topics_glob
189 Publish.topics_glob = RosbridgeUdpSocket.topics_glob
190 AdvertiseService.services_glob = RosbridgeUdpSocket.services_glob
191 UnadvertiseService.services_glob = RosbridgeUdpSocket.services_glob
192 CallService.services_glob = RosbridgeUdpSocket.services_glob
198 rospy.loginfo(
"Rosbridge UDP server started on port %d", port)
199 reactor.listenUDP(port, RosbridgeUdpFactory(), interface=interface)