H-a-y-K

Untitled

Mar 15th, 2020
154
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import PySide2.QtWidgets as QtWidgets
  2. import PySide2.QtCore as QtCore
  3. from PySide2.QtGui import QRegExpValidator
  4. import PySide2.QtWebSockets as QtWebSockets
  5. import PySide2.QtNetwork as QtNetwork
  6.  
  7. import logging
  8.  
  9.  
  10. class Client(QtCore.QObject):
  11.     UDP_SERVER_PORT = 45454
  12.     UDP_CLIENT_PORT = 45455
  13.  
  14.     def __init__(self, username, parent=None):
  15.         super().__init__(parent)
  16.         self.done = False
  17.         self.udpClient = QtNetwork.QUdpSocket(self)
  18.         self.webClient = QtWebSockets.QWebSocket(
  19.             "", QtWebSockets.QWebSocketProtocol.VersionLatest, self)
  20.  
  21.         self.udpClient.readyRead.connect(self.processPendingDatagrams)
  22.         self.webClient.connected.connect(self.onConnected)
  23.         self.webClient.textMessageReceived.connect(self.onTextMessageReceived)
  24.  
  25.         interfaces = QtNetwork.QNetworkInterface.allInterfaces()
  26.         for interface in interfaces:
  27.             if (interface.isValid() and
  28.                     not interface.flags() & QtNetwork.QNetworkInterface.IsLoopBack and
  29.                     not interface.flags() & QtNetwork.QNetworkInterface.IsPointToPoint and
  30.                     interface.flags() & QtNetwork.QNetworkInterface.IsUp and
  31.                     interface.flags() & QtNetwork.QNetworkInterface.IsRunning):
  32.                 addresses = interface.addressEntries()
  33.                 for address in addresses:
  34.                     if (not address.ip().isNull() and
  35.                             not address.ip().isLoopback() and
  36.                             address.ip().protocol() == QtNetwork.QAbstractSocket.IPv4Protocol and
  37.                             not address.broadcast().isNull()):
  38.                         logging.debug(f"Address: {str(address.broadcast())}")
  39.                         self.udpClient.bind(address.ip(), self.UDP_CLIENT_PORT,
  40.                                             QtNetwork.QUdpSocket.ShareAddress)
  41.                         self.udpClient.writeDatagram(QtCore.QByteArray("User = {}".format(username).encode()),
  42.                                                      address.broadcast(), self.UDP_SERVER_PORT)
  43.                         return
  44.  
  45.     @QtCore.Slot()
  46.     def processPendingDatagrams(self):
  47.         socket = self.sender()
  48.         logging.debug("Processing datagrams...")
  49.         while socket and socket.hasPendingDatagrams():
  50.             datagram = socket.receiveDatagram()
  51.             logging.debug(datagram.data())
  52.             logging.debug(datagram.data().startsWith(b"ws://"))
  53.             if datagram.data().startsWith(b"ws://"):
  54.                 self.webClient.open(QtCore.QUrl.fromEncoded(datagram.data()))
  55.  
  56.     @QtCore.Slot()
  57.     def onConnected(self):
  58.         logging.debug("Web client connected to server")
  59.  
  60.     @QtCore.Slot()
  61.     def onTextMessageReceived(self, mes):
  62.         logging.debug(mes)
RAW Paste Data