-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
127 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Defines the Connection class. | ||
|
||
import zlib | ||
import threading | ||
import json | ||
import copy | ||
import struct | ||
import rospy | ||
import time | ||
import socket | ||
|
||
# Represents a threaded websocket connection to the server. | ||
class Receiver(threading.Thread): | ||
|
||
def __init__(self, socket): | ||
super(Receiver, self).__init__() | ||
self.socket = socket | ||
self.values = dict() | ||
|
||
# Starts the Tornado IOLoop and connects to the websocket. | ||
# Called on thread start. | ||
def run(self): | ||
while True: | ||
data = self.socket.recv(65565) | ||
if data == "HANDSHAKE": | ||
continue | ||
self.process_message(data) | ||
|
||
# Returns the formatted last received message. | ||
def updates(self): | ||
#payloads = copy.copy(self.values) | ||
payloads = self.values | ||
self.values = dict() | ||
return payloads | ||
|
||
# Callback for message receiving. | ||
# Decompresses messages, converts to unicode, | ||
# and converts from JSON to dictionary. | ||
def process_message(self, payload): | ||
try: | ||
decompressed = zlib.decompress(payload) | ||
size = struct.unpack('=I', decompressed[:4]) | ||
frmt = "%ds" % size[0] | ||
unpacked = struct.unpack('=I' + frmt, decompressed) | ||
data = json.loads(unpacked[1]) | ||
self.values[data["Topic"]] = data | ||
except: | ||
pass |
Oops, something went wrong.