Example #1
0
 def publish_message(self, cname, messagetoclient):
     # Wait and then publish a message to the subscribed channel:
     time.sleep(10)
     grippub = GripPubControl({'control_uri': 'http://pushpin-svc:5561'})
     logger.info("Publishing the message " +  messagetoclient)
     print("Publishing the message " + messagetoclient)
     grippub.publish(cname, Item(WebSocketMessageFormat(messagetoclient)))
Example #2
0
 def publish_message(self):
     time.sleep(3)
     message = 'Please connect with Websocket. Http-stream is not supported yet!'
     print(message)
     grippub = GripPubControl({'control_uri': 'http://localhost:5561'})
     grippub.publish(self.cname, Item(WebSocketMessageFormat(message)))
            # enable GRIP
            out_headers.append(('Sec-WebSocket-Extensions', 'grip'))

            # ack the open
            out.append(e)

            # subscribe connection to channel
            cm = {'type': 'subscribe', 'channel': 'room'}
            out.append(WebSocketEvent('TEXT', 'c:%s' % json.dumps(cm)))
    elif e.type == 'CLOSE':
        out.append(e) # ack
        break
    elif e.type == 'TEXT':
        # broadcast to everyone
        pub.publish('room', Item(WebSocketMessageFormat(e.content)))

out_headers.append(('Content-Type', 'application/websocket-events'))

for header in out_headers:
    cm = {
        'type': 'setHeader',
        'payload': {
            'name': header[0],
            'value': header[1]
        }
    }
    sys.stderr.write('%s\n' % json.dumps(cm))

sys.stdout.write(encode_websocket_events(out))
Example #4
0
class PushpinExt(Connector):
    def get_connection_exception(self):
        return ServiceUnavailable

    def connect(self, **kwargs):

        variables = self.variables.copy()
        variables.update(kwargs)

        host = variables.get("host")
        port = variables.get("port")

        control_uri = f"http://{host}:{port}"
        self.pubctrl = GripPubControl({"control_uri": control_uri})

        is_active = self.publish_on_stream("admin",
                                           "Connection test",
                                           sync=True)

        if not is_active:
            raise ServiceUnavailable(f"Pushpin unavailable on {control_uri}")
        return self

    def disconnect(self) -> None:
        self.disconnected = True

    def is_connected(self) -> bool:
        log.warning("pushpin.is_connected method is not implemented")
        return not self.disconnected

    @staticmethod
    def callback(result, message):
        if result:
            log.debug("Message successfully published on pushpin")
        else:  # pragma: no cover
            log.error("Publish failed on pushpin: {}", message)

    def publish_on_stream(self, channel, message, sync=False):
        if not sync:
            self.pubctrl.publish_http_stream(channel,
                                             message,
                                             callback=PushpinExt.callback)
            return True

        try:
            self.pubctrl.publish_http_stream(channel, message, blocking=True)
            log.debug("Message successfully published on pushpin")
            return True
        except BaseException as e:
            log.error("Publish failed on pushpin: {}", message)
            log.error(e)
            return False

    def publish_on_socket(self, channel, message, sync=False):
        item = Item(WebSocketMessageFormat(message, binary=False))
        if not sync:
            self.pubctrl.publish(channel, item, callback=self.callback)
            return True

        try:
            self.pubctrl.publish(channel, item, blocking=True)
            log.debug("Message successfully published on pushpin")
            return True
        except BaseException as e:  # pragma: no cover
            log.error("Publish failed on pushpin: {}", message)
            log.error(e)
            return False
            # enable GRIP
            out_headers.append(('Sec-WebSocket-Extensions', 'grip'))

            # ack the open
            out.append(e)

            # subscribe connection to channel
            cm = {'type': 'subscribe', 'channel': 'room'}
            out.append(WebSocketEvent('TEXT', 'c:%s' % json.dumps(cm)))
    elif e.type == 'CLOSE':
        out.append(e)  # ack
        break
    elif e.type == 'TEXT':
        # broadcast to everyone
        pub.publish('room', Item(WebSocketMessageFormat(e.content)))

out_headers.append(('Content-Type', 'application/websocket-events'))

for header in out_headers:
    cm = {
        'type': 'setHeader',
        'payload': {
            'name': header[0],
            'value': header[1]
        }
    }
    sys.stderr.write('%s\n' % json.dumps(cm))

sys.stdout.write(encode_websocket_events(out))