cocos2d-lua3.7組件篇(三)-http通信demo

步驟:
客戶端 -----------Post 用戶名和密碼
服務端接受Post請求,讀取數據,返回response
一、客戶端代碼
loadingImg = require"app.scenes.LoadingLayer"
local LoginScene = class("LoginScene", function()
return display.newScene("LoginScene")
end)
function LoginScene:ctor()
print("LoginScene")
self.loading = loadingImg:new()
self.loading:addTo(self)
self:removeChild(self.loading)
local function onRequestCallback(event)
local request = event.request
--dump(event)
if event.name == "completed" then
print(request:getResponseHeadersString())
local code = request:getResponseStatusCode()
if code ~= 200 then
-- 請求結束,但沒有返回 200 響應代碼
print(code)
return
end
print("---------------callback--------")
-- 請求成功,顯示服務端返回的內容
print("response length" .. request:getResponseDataLength())
local response = request:getResponseString()
print(response)
elseif event.name == "progress" then
print("progress" .. event.dltotal)
else
-- 請求失敗,顯示錯誤代碼和錯誤消息
print(event.name)
print(request:getErrorCode(), request:getErrorMessage())
return
end
end
local request = network.createHTTPRequest(onRequestCallback, "127.0.0.1:19999", "POST")
--request:addPOSTValue("name", "laoliu")
request:setPOSTData("user:123456,password:123456")
request:start()
end
function LoginScene:onEnter()
end
function LoginScene:onExit()
end
return LoginScene
二、客戶端lua代碼核心介紹
network.createHTTPRequest(onRequestCallback, "127.0.0.1:19999", "POST")
--request:addPOSTValue("name", "laoliu")
request:setPOSTData("user:123456,password:123456")
三、服務端QT代碼
在.pro文件中追加??? QT+= core gui network
.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void onNewConnection();
void acceptConnection();
void readMessage();
void disconnected();
void deleteLater();
private:
Ui::MainWindow *ui;
QTcpServer * serverListen;
QTcpSocket *serverConnect;
};
#endif // MAINWINDOW_H
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
serverListen = new QTcpServer;
serverConnect = new QTcpSocket;
serverListen->listen(QHostAddress::Any,19999);
connect(serverListen,SIGNAL(newConnection()),this,SLOT(acceptConnection()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onNewConnection()
{
int temp=1;
}
void MainWindow::acceptConnection()
{
serverConnect = serverListen->nextPendingConnection(); //得到每個連進來的socket
connect(serverConnect,SIGNAL(readyRead()),this,SLOT(readMessage())); //有可讀的信息,觸發讀函數
}
void MainWindow::readMessage() //讀取信息
{
// ui->textEdit_rec->te
QByteArray qba= serverConnect->readAll(); //讀取
qDebug()< QString ss=QVariant(qba).toString(); QString info(ss); QStringList tokens(info.split( QRegExp("[ /r/n][ /r/n]*"))); qDebug()< if ( tokens[0] == "GET" ) //getDeal(serverConnect); { qDebug()<<"get";} if( tokens[0] == "POST") //postDeal(serverConnect); { qDebug()<<"POST";} serverConnect->write("HTTP/1.1 200 OK\r\n"); serverConnect->close(); } void MainWindow::disconnected() { qDebug()<<"disconnected"; } void MainWindow::deleteLater() { qDebug()<<"deleteLater"; } 核心基于,這兩個類進行tcp/ip 操作 QTcpServer * serverListen; QTcpSocket *serverConnect; QTcpServer的基本操作: 1、調用listen監聽端口。 2、連接信號newConnection,在槽函數里調用nextPendingConnection獲取連接進來的socket。 QTcpSocket的基本能操作: 1、調用connectToHost連接服務器。 2、調用waitForConnected判斷是否連接成功。 3、連接信號readyRead槽函數,異步讀取數據。 4、調用waitForReadyRead,阻塞讀取數據。 四、tcp、ip實現http的過程: HTTP通信機制是在一次完整的HTTP通信過程中,Web瀏覽器與Web服務器之間將完成下列7個步驟: 1. 建立TCP連接 2. Web瀏覽器向Web服務器發送請求命令 3. Web瀏覽器發送請求頭信息 4. Web服務器應答 5. Web服務器發送應答頭信息 6. Web服務器向瀏覽器發送數據 7. Web服務器關閉TCP連接 Cocos2D HTTP Lua TCP/IP
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。