|
23 | 23 | class AutobahnRosBridgeProtocol(RosBridgeProtocol, WebSocketClientProtocol):
|
24 | 24 | def __init__(self, *args, **kwargs):
|
25 | 25 | super(AutobahnRosBridgeProtocol, self).__init__(*args, **kwargs)
|
| 26 | + self.headers = {} |
26 | 27 |
|
27 | 28 | def onConnect(self, response):
|
28 | 29 | LOGGER.debug("Server connected: %s", response.peer)
|
29 | 30 |
|
| 31 | + def getHandshakeRequestHeaders(self): |
| 32 | + headers = super(AutobahnRosBridgeProtocol, self).getHandshakeRequestHeaders() |
| 33 | + for key, value in self.headers.items(): |
| 34 | + headers.append((key, value)) |
| 35 | + return headers |
| 36 | + |
30 | 37 | def onOpen(self):
|
31 | 38 | LOGGER.info("Connection to ROS ready.")
|
32 | 39 | self._manual_disconnect = False
|
@@ -62,13 +69,20 @@ class AutobahnRosBridgeClientFactory(EventEmitterMixin, ReconnectingClientFactor
|
62 | 69 |
|
63 | 70 | protocol = AutobahnRosBridgeProtocol
|
64 | 71 |
|
65 |
| - def __init__(self, *args, **kwargs): |
| 72 | + def __init__(self, *args, headers=None, **kwargs): |
66 | 73 | super(AutobahnRosBridgeClientFactory, self).__init__(*args, **kwargs)
|
| 74 | + self.headers = headers or {} |
67 | 75 | self._proto = None
|
68 | 76 | self._manager = None
|
69 | 77 | self.connector = None
|
70 | 78 | self.setProtocolOptions(closeHandshakeTimeout=5)
|
71 | 79 |
|
| 80 | + def buildProtocol(self, addr): |
| 81 | + proto = self.protocol() |
| 82 | + proto.factory = self |
| 83 | + proto.headers = self.headers |
| 84 | + return proto |
| 85 | + |
72 | 86 | def connect(self):
|
73 | 87 | """Establish WebSocket connection to the ROS server defined for this factory."""
|
74 | 88 | self.connector = connectWS(self)
|
|
0 commit comments