Пример #1
0
class ChatHandler(WebSocketHandler):
    def __init__(self, *args, **kwargs):
        super(ChatHandler, self).__init__(*args, **kwargs)
        self.listen()

    @tornado.gen.engine
    def listen(self):
        self.redis_client = Redis()
        self.redis_client.connect()
        yield tornado.gen.Task(self.redis_client.subscribe, 'test_channel')
        self.redis_client.subscribed = True
        self.redis_client.listen(self.on_messages_published)

    def open(self):
        logging.info("on new cline")

    def on_messages_published(self, message):
        if not isinstance(message, unicode):
            if isinstance(message, tornadoredis.exceptions.RequestError):
                self.write_message(message.message)
            else:
                if message.kind == "subscribe":
                    self.write_message("进入%s号房间" % (str(message.channel)))
                if message.kind == 'message':
                    self.write_message(str(message.body))
                if message.kind == 'disconnect':
                    # Do not try to reconnect, just send a message back
                    # to the client and close the client connection
                    self.write_message('The connection terminated '
                                       'due to a Redis server error.')
                    self.close()

    def on_message(self, message):
        logging.info('Received new message %r', message)
        try:
            # Convert to JSON-literal.
            message_encoded = tornado.escape.json_encode(message)
            # Persistently store message in Redis.
            # Publish message in Redis channel.
            redis = Redis()
            redis.publish('test_channel',message_encoded)
            self.write_message(message_encoded)
        except Exception, err:
            e = str(sys.exc_info()[0])
            # Send an error back to client.
            self.write_message({'error': 1, 'textStatus': 'Error writing to database: ' + str(err)})
            return

        # Send message through the socket to indicate a successful operation.
        self.write_message(message)
        return
Пример #2
0
 def on_message(self, message):
     logging.info('Received new message %r', message)
     try:
         # Convert to JSON-literal.
         message_encoded = tornado.escape.json_encode(message)
         # Persistently store message in Redis.
         # Publish message in Redis channel.
         redis = Redis()
         redis.publish('test_channel',message_encoded)
         self.write_message(message_encoded)
     except Exception, err:
         e = str(sys.exc_info()[0])
         # Send an error back to client.
         self.write_message({'error': 1, 'textStatus': 'Error writing to database: ' + str(err)})
         return
Пример #3
0
class RedisService:
    def __init__(self, config):
        self._redis = Redis(redis_config=config)

    def get_convert_arpabet(self, word):
        if not word:
            return None
        key = get_rapabet_redis_key(word=word)
        return self._redis.get_data(key)

    def save_convert_arpabet(self, model):
        if not model:
            return None

        key = get_rapabet_redis_key(word=model.word)
        return self._redis.set_data(key=key, data=model.get_default_arpabet())

    # 爬回来的音标
    def get_crawl_alphabet(self, word):
        if not word:
            return None
        key = get_crawl_redis_key(word=word)
        return self._redis.get_data(key)

    # 爬回来的音标
    def save_crawl_alphabet(self, model):
        if not model:
            return None
        key = get_crawl_redis_key(word=model.get("word"))
        return self._redis.set_data(
            key=key, data=model.get("american_phonetic_alphabet"))

    def delete_crawl_alphabet(self, word):
        if not word:
            return None
        key = get_crawl_redis_key(word=word)
        return self._redis.delete(key=key)
Пример #4
0
class TestRedis(TestCase):

    def setUp(self):
        self.redis = Redis({'host': '127.0.0.1',
                            'port': 6379,
                            'db': 0,
                            'password': ''
                            })

    def test_save_update_delete(self):
        self.redis.set_data("1", "哈哈")
        self.assertEqual(self.redis.get_data("1"), "哈哈")
        self.assertFalse(self.redis.get_data("1") == "哈哈2")
        self.redis.delete("1")
        self.assertFalse(self.redis.get_data("1"))
Пример #5
0
 def __init__(self, config):
     self._redis = Redis(redis_config=config)
Пример #6
0
 def setUp(self):
     self.redis = Redis({'host': '127.0.0.1',
                         'port': 6379,
                         'db': 0,
                         'password': ''
                         })
Пример #7
0
 def listen(self):
     self.redis_client = Redis()
     self.redis_client.connect()
     yield tornado.gen.Task(self.redis_client.subscribe, 'test_channel')
     self.redis_client.subscribed = True
     self.redis_client.listen(self.on_messages_published)