# -*- coding: utf-8 -*-
# @Time    : 2019/9/5 13:42
# @Author  : duofeng Wu
# @Site    :
# @File    : llr.py
# @Software: PyCharm

import json
from ws4py.client.threadedclient import WebSocketClient
from clients.core2 import MSGHandler2


class ExampleClient(WebSocketClient):

    def __init__(self, url):
        super().__init__(url)
        self.name = "Agent1"
        self.msg_handler = MSGHandler2()

    def opened(self):
        pass

    def closed(self, code, reason=None):
        print("Connect closed down", code, reason)

    def received_message(self, message):
        msg = self.msg_handler.handle(json.loads(str(message)))
        if msg:
            self.send(json.dumps(msg))


if __name__ == '__main__':
    try:
        ws = ExampleClient('ws://192.168.1.116:23456/game/llr2')

        ws.connect()
        ws.run_forever()
    except KeyboardInterrupt:
        ws.close()
