gpt4 book ai didi

python - PyQt5 中的 WebSocket 服务器,显然是正确的,但我无法连接

转载 作者:太空宇宙 更新时间:2023-11-03 18:00:23 25 4
gpt4 key购买 nike

我需要在pyqt5中创建一个websocket服务器,阅读5.3版本的文档,其中有所有必要的类,请按照C++中的这些示例 http://doc.qt.io/qt-5/qtwebsockets-examples.html这就是结果:

from PyQt5 import QtCore, QtWebSockets,  QtNetwork

class MyServer(QtCore.QObject):
def __init__(self, parent=None):
super(QtCore.QObject, self).__init__(parent)
self.server = QtWebSockets.QWebSocketServer('My Socket', QtWebSockets.QWebSocketServer.NonSecureMode)
if self.server.listen(QtNetwork.QHostAddress.LocalHost, 1302):
print('Connected: '+self.server.serverName()+' : '+self.server.serverAddress().toString()+':'+str(self.server.serverPort()))
else:
print('error')
self.server.newConnection.connect(self.onNewConnection)

print(self.server.isListening())

def onNewConnection(self):
print(self.sender())
print("inside")
self.server.textMessageReceived.connect(self.processTextMessage)
self.server.binaryMessageReceived.connect(self.processBinaryMessage)
self.server.disconnected.connect(self.socketDisconnected)

def processTextMessage(self, message):
print(message)

def processBinaryMessage(self, message):
print(message)

def socketDisconnected(self):
print('out')

if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MyServer()
sys.exit(app.exec_())

运行 isListening 方法时,返回 true,这让我相信这段代码是正确的,但是我尝试通过 javascript 客户端(html5 websocket)进行连接,它返回一个服务器连接错误,当我尝试拍摄时也会发生同样的情况通过telnet连接服务器(telnet localhost 1302)。

最佳答案

感谢您的回复,代码中还有其他错误,如果有人需要,最终代码:

from PyQt5 import QtCore, QtWebSockets,  QtNetwork, QtWidgets

class MyServer(QtCore.QObject):
def __init__(self, parent):
super(QtCore.QObject, self).__init__(parent)
self.clients = []
self.server = QtWebSockets.QWebSocketServer(parent.serverName(), parent.secureMode(), parent)
if self.server.listen(QtNetwork.QHostAddress.LocalHost, 1302):
print('Connected: '+self.server.serverName()+' : '+self.server.serverAddress().toString()+':'+str(self.server.serverPort()))
else:
print('error')
self.server.newConnection.connect(self.onNewConnection)

print(self.server.isListening())

def onNewConnection(self):
self.clientConnection = self.server.nextPendingConnection()
self.clientConnection.textMessageReceived.connect(self.processTextMessage)

self.clientConnection.binaryMessageReceived.connect(self.processBinaryMessage)
self.clientConnection.disconnected.connect(self.socketDisconnected)

self.clients.append(self.clientConnection)

def processTextMessage(self, message):
if (self.clientConnection):
self.clientConnection.sendTextMessage(message)

def processBinaryMessage(self, message):
if (self.clientConnection):
self.clientConnection.sendBinaryMessage(message)

def socketDisconnected(self):
if (self.clientConnection):
self.clients.remove(self.clientConnection)
self.clientConnection.deleteLater()


if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
serverObject = QtWebSockets.QWebSocketServer('My Socket', QtWebSockets.QWebSocketServer.NonSecureMode)
server = MyServer(serverObject)
serverObject.closed.connect(app.quit)
app.exec_()

关于python - PyQt5 中的 WebSocket 服务器,显然是正确的,但我无法连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27767985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com