Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import PySide2.QtWidgets as QtWidgets
- import PySide2.QtCore as QtCore
- from PySide2.QtGui import QRegExpValidator
- import PySide2.QtWebSockets as QtWebSockets
- import PySide2.QtNetwork as QtNetwork
- import hichess.hichess as hichess
- import logging
- class Client(QtCore.QObject):
- UDP_SERVER_PORT = 45454
- UDP_CLIENT_PORT = 45455
- def __init__(self, username, parent=None):
- super().__init__(parent)
- self.done = False
- self.udpClient = QtNetwork.QUdpSocket(self)
- self.webClient = QtWebSockets.QWebSocket(
- "", QtWebSockets.QWebSocketProtocol.VersionLatest, self)
- self.udpClient.readyRead.connect(self.processPendingDatagrams)
- self.webClient.connected.connect(self.onConnected)
- self.webClient.textMessageReceived.connect(self.onTextMessageReceived)
- interfaces = QtNetwork.QNetworkInterface.allInterfaces()
- for interface in interfaces:
- if (interface.isValid() and
- not interface.flags() & QtNetwork.QNetworkInterface.IsLoopBack and
- not interface.flags() & QtNetwork.QNetworkInterface.IsPointToPoint and
- interface.flags() & QtNetwork.QNetworkInterface.IsUp and
- interface.flags() & QtNetwork.QNetworkInterface.IsRunning):
- addresses = interface.addressEntries()
- for address in addresses:
- if (not address.ip().isNull() and
- not address.ip().isLoopback() and
- address.ip().protocol() == QtNetwork.QAbstractSocket.IPv4Protocol and
- not address.broadcast().isNull()):
- logging.debug(f"Address: {str(address.broadcast())}")
- self.udpClient.bind(address.ip(), self.UDP_CLIENT_PORT,
- QtNetwork.QUdpSocket.ShareAddress)
- self.udpClient.writeDatagram(QtCore.QByteArray("User = {}".format(username).encode()),
- address.broadcast(), self.UDP_SERVER_PORT)
- return
- @QtCore.Slot()
- def processPendingDatagrams(self):
- socket = self.sender()
- logging.debug("Processing datagrams...")
- while socket and socket.hasPendingDatagrams():
- datagram = socket.receiveDatagram()
- logging.debug(datagram.data())
- logging.debug(datagram.data().startsWith(b"ws://"))
- if datagram.data().startsWith(b"ws://"):
- self.webClient.open(QtCore.QUrl.fromEncoded(datagram.data()))
- @QtCore.Slot()
- def onConnected(self):
- logging.debug("Web client connected to server")
- @QtCore.Slot()
- def onTextMessageReceived(self, mes):
- logging.debug(mes)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement