Skip to content

Commit

Permalink
Merge pull request RobotWebTools#12 from pablisho/bugs
Browse files Browse the repository at this point in the history
Changed to CurlHTTPClient to resolve timeout bug
  • Loading branch information
Julian Cerruti committed Mar 25, 2015
2 parents 95a9d01 + 51e17c1 commit d71ea09
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions rosbridge_server/scripts/rosbridge_websocket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tornado.httpclient
import base64
import urllib
import time

from tornado.websocket import websocket_connect
from tornado.ioloop import IOLoop
Expand Down Expand Up @@ -37,10 +38,14 @@ def __init__(self, uri):
self.doconn()

def doconn(self):
rospy.loginfo("trying connection to %s" % (self.uri,))
w = websocket_connect(self.uri)
rospy.loginfo("connected, waiting for messages")
w.add_done_callback(self.wsconnection_cb)
try:
rospy.loginfo("trying connection to %s" % (self.uri,))
w = websocket_connect(self.uri)
rospy.loginfo("connected, waiting for messages")
w.add_done_callback(self.wsconnection_cb)
except Exception as e:
rospy.logerror(e)
rospy.logerror("There was an exception")

def dokeepalive(self):
stream = self.conn.protocol.stream
Expand All @@ -65,14 +70,14 @@ def message(self, _message):
if msg['op'] == 'video':
try:
args = msg['args']
c = VideoTransfer("http://localhost:8080/stream", args, self)
self.transfer = VideoTransfer("http://localhost:8080/stream", args, self)
except e:
print "Could not connect to WebCam"
print e
rospy.loginfo("Could not connect to WebCam")
rospy.logerror("Could not connect to WebCam")
rospy.logerror(e)
self.send_message('{"op":"endVideo"}')
elif msg['op'] == "endVideo":
#TODO Resolve stop from client
self.transfer.endVideo()
pass
else:
protocol.incoming(_message)
Expand All @@ -91,29 +96,35 @@ def send_message(self, _message):

class VideoTransfer():
def __init__(self, url, args, connection):
tornado.httpclient.AsyncHTTPClient.configure(
"tornado.curl_httpclient.CurlAsyncHTTPClient")
self.conn = connection
url = url + "?" + urllib.urlencode(args)
url = url.replace("%2F","/")
req = tornado.httpclient.HTTPRequest(
url = url,
streaming_callback = self.streaming_callback)
streaming_callback = self.streaming_callback,
connect_timeout = 0.0,
request_timeout = 0.0)
http_client = tornado.httpclient.AsyncHTTPClient()
http_client.fetch(req, self.async_callback)
print "fetch finished"
self.start = time.time()

def streaming_callback(self, data):
"Sends video in chunks"
try:
#print "Sending Video chunks"
encoded = base64.b64encode(data) # Encode in Base64 & make json
chunk = json.dumps({"op": "video", "data": encoded})
self.conn.send_message(chunk)
except Exception as e:
print e

def async_callback(self, response):
self.conn.finish()
print "Finished connection"

def end_video(self):
#TODO Manage end of video transfer
pass

if __name__ == "__main__":
try:
Expand Down

0 comments on commit d71ea09

Please sign in to comment.