| 47 | |
| 48 | |
| 49 | class WebChannelObject(QObject): |
| 50 | |
| 51 | def __init__(self, *args, **kwargs): |
| 52 | super(WebChannelObject, self).__init__(*args, **kwargs) |
| 53 | # 内部属性供外部调用 |
| 54 | self._intValue = 0 |
| 55 | self._floatValue = 0.0 |
| 56 | self._boolValue = False |
| 57 | self._strValue = '' |
| 58 | # 设置数组或者字典有一定问题 |
| 59 | # self._listValue = [] |
| 60 | # self._mapValue = {} |
| 61 | |
| 62 | # webchannel对象 |
| 63 | self.m_webchannel = QWebChannel(self) |
| 64 | # 这里默认注册自己,这里使用了类名作为名称 |
| 65 | self.registerObject(self.__class__.__name__, self) |
| 66 | # websocket服务 |
| 67 | self.m_clients = {} |
| 68 | self.m_server = QWebSocketServer(self.__class__.__name__, |
| 69 | QWebSocketServer.NonSecureMode, self) |
| 70 | |
| 71 | def registerObject(self, name, obj): |
| 72 | """注册对象 |
| 73 | @param name: 名称 |
| 74 | @type name: str |
| 75 | @param obj: 对象 |
| 76 | @type obj: QObject |
| 77 | """ |
| 78 | self.m_webchannel.registerObject(name, obj) |
| 79 | |
| 80 | def registerObjects(self, objects): |
| 81 | """注册多个对象 |
| 82 | @param objects: 对象列表 |
| 83 | @type objects: list |
| 84 | """ |
| 85 | for name, obj in objects: |
| 86 | self.registerObject(name, obj) |
| 87 | |
| 88 | def deregisterObject(self, obj): |
| 89 | """注销对象 |
| 90 | @param obj: 对象 |
| 91 | @type obj: QObject |
| 92 | """ |
| 93 | self.m_webchannel.deregisterObject(obj) |
| 94 | |
| 95 | def deregisterObjects(self, objects): |
| 96 | """注销多个对象 |
| 97 | @param objects: 对象列表 |
| 98 | @type objects: list |
| 99 | """ |
| 100 | for obj in objects: |
| 101 | self.deregisterObject(obj) |
| 102 | |
| 103 | def start(self, port=12345): |
| 104 | """启动服务 |
| 105 | @param port: 端口 |
| 106 | @type port: int |