SHARE
TWEET

Untitled

a guest Jul 7th, 2017 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import sys
  3.  
  4. from PyQt5.QtCore import Qt, QEvent, QObject, QFile, QIODevice, QJsonDocument, QJsonParseError, pyqtSlot, pyqtSignal
  5. from PyQt5 import QtCore, QtGui, QtWidgets
  6. from PyQt5.QtWebEngineWidgets import QWebEngineProfile, QWebEngineScript
  7. from PyQt5.QtWebChannel import QWebChannel, QWebChannelAbstractTransport
  8. from PyQt5.QtWebSockets import QWebSocketServer
  9. from PyQt5.QtNetwork import QHostAddress
  10.  
  11. class CallHandler(QObject):
  12.     @pyqtSlot()
  13.     def test(self):
  14.         print("call received")
  15.  
  16. class WebSocketTransport(QWebChannelAbstractTransport):
  17.     _socket = None
  18.  
  19.     def __init__(self, socket):
  20.         super().__init__(socket)
  21.  
  22.         self._socket = socket
  23.         self._socket.textMessageReceived.connect(self.textMessageReceived)
  24.  
  25.     def sendMessage(self, message):
  26.         print("sendMessage")
  27.  
  28.         doc = QJsonDocument(message)
  29.         self._socket.sendTextMessage(doc.toJson(QJsonDocument.Compact).decode("utf-8"))
  30.  
  31.     @pyqtSlot(str)
  32.     def textMessageReceived(self, messageData):
  33.         parser_error = QJsonParseError()
  34.         message = QJsonDocument.fromJson(messageData.encode("utf-8"), parser_error)
  35.  
  36.         if parser_error.error != QJsonParseError.NoError:
  37.             print("1")
  38.  
  39.         elif not message.isObject():
  40.             print("2")
  41.  
  42.         else:
  43.             print("MSG")
  44.             a = {}
  45.             a.update(message.object())
  46.             import dbg; dbg.pyqt_set_trace()
  47.             self.messageReceived.emit(a, self)
  48.             print("MSG OK")
  49.  
  50. class WebSocketClientWrapper(QObject):
  51.     _server = None
  52.  
  53.     clientConnected = pyqtSignal(QWebChannelAbstractTransport)
  54.  
  55.     def __init__(self, server, parent=None):
  56.         super().__init__(parent)
  57.  
  58.         self._server = server
  59.         self._server.newConnection.connect(self.handleNewConnection)
  60.  
  61.     @pyqtSlot()
  62.     def handleNewConnection(self):
  63.         print("CON")
  64.         self.clientConnected.emit(WebSocketTransport(self._server.nextPendingConnection()))
  65.  
  66. class Browser(QtWidgets.QMainWindow):
  67.     _ui = None
  68.     _channel = None
  69.     _handler = None
  70.     _server = None
  71.     _client_wrapper = None
  72.    
  73.  
  74.     def __init__(self):
  75.         QtWidgets.QMainWindow.__init__(self)
  76.  
  77.         self._install_js()
  78.  
  79.         self._ui = Ui_Browser()
  80.         self._ui.setupUi(self)
  81.  
  82.         self._channel = QWebChannel(self)
  83.         self._handler = CallHandler(self._channel)
  84.         self._channel.registerObject("self._handler", self._handler)
  85.  
  86.         self._server = QWebSocketServer("Server", QWebSocketServer.NonSecureMode, self)
  87.         self._client_wrapper = WebSocketClientWrapper(self._server, self._server)
  88.         self._client_wrapper.clientConnected.connect(self._channel.connectTo)
  89.         self._server.listen(QHostAddress.LocalHost, 12345)
  90.  
  91.         self._ui.web_view.page().setWebChannel(self._channel)
  92.  
  93.     def _install_js(self):
  94.         self._inject_js(":/qtwebchannel/qwebchannel.js", QWebEngineScript.DocumentCreation)
  95.         self._inject_js(os.path.join(BASE_DIR, "app.js"))
  96.  
  97.     def _inject_js(self, path, injection_point=QWebEngineScript.DocumentReady):
  98.         profile = QWebEngineProfile.defaultProfile()
  99.  
  100.         js = QWebEngineScript()
  101.         js.setName(os.path.basename(path))
  102.  
  103.         js_file = QFile(path)
  104.         js_file.open(QIODevice.ReadOnly)
  105.  
  106.         js.setSourceCode(bytes(js_file.readAll()).decode("utf-8"))
  107.  
  108.         # TODO: check if it's possible to switch to ApplicationWorld
  109.         # probably we need a different transport socket
  110.         # https://bugreports.qt.io/browse/QTBUG-50318
  111.         js.setWorldId(QWebEngineScript.ApplicationWorld)
  112.  
  113.         js.setInjectionPoint(injection_point)
  114.         js.setRunsOnSubFrames(False)
  115.         profile.scripts().insert(js)
  116.  
  117. if __name__ == "__main__":
  118.     app = QtWidgets.QApplication(sys.argv)
  119.  
  120.     main = Browser()
  121.     main.show()
  122.  
  123.     sys.exit(app.exec_())
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top