-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
132 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "websocketserver.h" | ||
#include <QMessageBox> | ||
QMap<QString,QWebSocket*> WebSocketServer::clientMap; | ||
WebSocketServer* WebSocketServer::instance=NULL; | ||
WebSocketServer::WebSocketServer(QWidget *parent):QWidget(parent) | ||
{ | ||
instance=this; | ||
} | ||
|
||
void WebSocketServer::run(){ | ||
webSocketServer=new QWebSocketServer("Server",QWebSocketServer::NonSecureMode); | ||
if (!webSocketServer->listen(QHostAddress::LocalHost, 12345)) { | ||
qFatal("Failed to open web socket server."); | ||
QMessageBox::warning(this,"警告","12345端口已被占用"); | ||
return; | ||
} | ||
connect(webSocketServer,&QWebSocketServer::newConnection,this,&WebSocketServer::newConnection); | ||
} | ||
|
||
void WebSocketServer::newConnection(){ | ||
QWebSocket *websocket=webSocketServer->nextPendingConnection(); | ||
if(!websocket){ | ||
return; | ||
} | ||
connect(websocket,&QWebSocket::textMessageReceived,this,[=](const QString& msg){ | ||
clientMap[msg]=websocket; | ||
qDebug() << "新增客户端" << clientMap.count(); | ||
},Qt::QueuedConnection); | ||
|
||
connect(websocket,&QWebSocket::disconnected,this,[=](){ | ||
qDebug() << "客户端退出"; | ||
}); | ||
|
||
} | ||
QWebSocket* WebSocketServer::getClient(QString clientId){ | ||
return clientMap[clientId]; | ||
} | ||
|
||
void WebSocketServer::deleteClient(QString clientId){ | ||
clientMap.remove(clientId); | ||
} | ||
|
||
void WebSocketServer::sendMsg(QString clientId,QString msg){ | ||
// qDebug() << "客户端数量" << clientMap.count(); | ||
clientMap[clientId]->sendTextMessage(msg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef WEBSOCKETSERVER_H | ||
#define WEBSOCKETSERVER_H | ||
|
||
#include <QWidget> | ||
#include <QWebSocketServer> | ||
#include <QWebSocket> | ||
class WebSocketServer:public QWidget | ||
{ | ||
Q_OBJECT | ||
public: | ||
WebSocketServer(QWidget *parent); | ||
|
||
static WebSocketServer* instance; | ||
|
||
void run(); | ||
|
||
static QWebSocket* getClient(QString clientId); | ||
|
||
static void deleteClient(QString clientId); | ||
|
||
void sendMsg(QString clientId,QString msg); | ||
|
||
private: | ||
QWebSocketServer* webSocketServer; | ||
static QMap<QString,QWebSocket*> clientMap; | ||
|
||
public Q_SLOTS: | ||
void newConnection(); | ||
|
||
signals: | ||
void sendMessage(const QString& msg); | ||
}; | ||
|
||
#endif // WEBSOCKETSERVER_H |