本篇文章給大家分享的是有關如何使用HTTP實現(xiàn)二進制傳輸,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:主機域名、虛擬主機、營銷軟件、網站建設、烈山網站維護、網站推廣。HTTP實現(xiàn)二進制傳輸。曾經為這事還糾結過。剛工作時游戲都是端游。基本學習和接觸過得都是TCP發(fā)送二進制方式。當時剛開始轉型研發(fā)頁游。原來刪除的都是C++開發(fā)。突然要研究flash搞網頁游戲。ActionScript(簡稱AS)這語言不熟悉。弄網絡封裝時肯定潛意識設計思路是HTTP實現(xiàn)二進制傳輸。然后結合網絡消息采用工具自動化生成方式。自己不善于HTTP。故意招個比較擅長的人協(xié)助搞下底層。我把上面想法告訴他。他斬釘截鐵告訴我HTTP肯定無法傳輸二進制。這讓我不爽研究好幾天終于搞出來了。有時成長需要時間需要去沉淀。經歷過就對如何解決記憶更加深刻。
同理。HTTP傳輸依然靠Gk8HttpClient類。具體實現(xiàn)參閱。 網絡通信3:HTTP實現(xiàn)文本傳輸。炮彈抽象轉實體就是Gk8ByteMaker。 網絡通信1:字節(jié)流的封裝。實現(xiàn)Gk8HttpService類提供發(fā)送和接收。設置HttpUrl,設置消息Header,是否壓縮等API。方便邏輯應用直接使用。
C++實現(xiàn)HttpService類:Gk8HttpService.h
#ifndef __GK8HTTPSERVICE_H__ #define __GK8HTTPSERVICE_H__ #pragma once #include "curl/curl.h" #include "Gk8HttpClient.h" #include "Gk8HttpRequest.h" #include "Gk8HttpResponse.h" class Gk8HttpService:public Gk8BaseObj { DECLARE_TOSPP_MAP; private: Gk8Str m_sHttpUrl; //[HTTP請求地址] Gk8ByteMaker m_iRequestData; //[HTTP請求數(shù)據(jù)] GK8_BOOL m_bUseZip; //[HTTP消息使用壓縮] Gk8Str m_sUserId; //[用戶ID] Gk8Str m_sUniqueKey; //[緩存唯一KEY] GK8_VOID HttpRequestStarted(); GK8_VOID HttpRequestCompleted(GK8_LPVOID lpHttpResponse); public: Gk8HttpService(); ~Gk8HttpService(); static Gk8HttpService* Create(Gk8BaseObj* pSuperObj=NULL); GK8_VOID Destroy(); GK8_VOID TOSPPFUNC SetHttpUrl(GK8_LPCSTR lpHttpUrl); GK8_VOID TOSPPFUNC SetHeader(GK8_LPCSTR lpUserId,GK8_LPCSTR lpUniqueKey); GK8_VOID TOSPPFUNC SetUseZip(GK8_BOOL bUseZip); GK8_VOID TOSPPFUNC SendMessage(Gk8ByteMaker* pRequestData); GK8_VOID TOSPPFUNC PutMessage(Gk8ByteMaker* pRequestData); GK8_VOID TOSPPFUNC SendCachedMessage(); }; #endif
C++實現(xiàn)HttpService類:Gk8HttpService.cpp
#include "Gk8HttpService.h" #include "Gk8OperSys.h" #include "Gk8Helper.h" #define segment_size 1024*100 static Gk8Str sg_ReceiveMessageFun("ReceiveMessage");//[接收網絡信息] /////////////////////////////////////////////CLASS-TOLUA//////////////////////////////////////////////////// TOLUA_CLASS_COLLECT_FUNC(Gk8HttpService) BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,Create) if(!CheckToLuaFunParam(L,"Create",Gk8Var()<<ISUSERTABLE("Gk8HttpService"))) return 0; Gk8BaseObj* pSuperObj=NULL; if(IFUSERTYPE(2,Gk8BaseObj)) pSuperObj=TOLUAGETOBJ(Gk8BaseObj,2); Gk8HttpService* pHttpService=Gk8HttpService::Create(pSuperObj); toluafix_pushusertype_ccobject(L,TOLUAOBJID(pHttpService),TOLUALPLUAID(pHttpService),(GK8_LPVOID)pHttpService,"Gk8HttpService"); END_TOLUA_CLASS_FUNC BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,SetHttpUrl) if(!CheckToLuaFunParam(L,"SetHttpUrl",Gk8Var()<<ISUSERTYPE("Gk8HttpService")<<ISSTRING<<ISNOOBJ)) return 0; TOLUAGETOBJ(Gk8HttpService,1)->SetHttpUrl(TOLUAGETSTRING(2)); END_TOLUA_CLASS_FUNC BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,SetHeader) if(!CheckToLuaFunParam(L,"SetHeader",Gk8Var()<<ISUSERTYPE("Gk8HttpService")<<ISSTRING<<ISSTRING<<ISNOOBJ)) return 0; TOLUAGETOBJ(Gk8HttpService,1)->SetHeader(TOLUAGETSTRING(2),TOLUAGETSTRING(3)); END_TOLUA_CLASS_FUNC BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,SetUseZip) if(!CheckToLuaFunParam(L,"SetUseZip",Gk8Var()<<ISUSERTYPE("Gk8HttpService")<<ISBOOLEAN<<ISNOOBJ)) return 0; TOLUAGETOBJ(Gk8HttpService,1)->SetUseZip(TOLUAGETBOOLEAN(2)); END_TOLUA_CLASS_FUNC BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,SendMessage) if(!CheckToLuaFunParam(L,"SendMessage",Gk8Var()<<ISUSERTYPE("Gk8HttpService")<<ISUSERTYPE("Gk8ByteMaker")<<ISNOOBJ)) return 0; TOLUAGETOBJ(Gk8HttpService,1)->SendMessage(TOLUAGETOBJ(Gk8ByteMaker,2)); END_TOLUA_CLASS_FUNC BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,PutMessage) if(!CheckToLuaFunParam(L,"PutMessage",Gk8Var()<<ISUSERTYPE("Gk8HttpService")<<ISUSERTYPE("Gk8ByteMaker")<<ISNOOBJ)) return 0; TOLUAGETOBJ(Gk8HttpService,1)->PutMessage(TOLUAGETOBJ(Gk8ByteMaker,2)); END_TOLUA_CLASS_FUNC BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,SendCachedMessage) if(!CheckToLuaFunParam(L,"SendCachedMessage",Gk8Var()<<ISUSERTYPE("Gk8HttpService")<<ISNOOBJ)) return 0; TOLUAGETOBJ(Gk8HttpService,1)->SendCachedMessage(); END_TOLUA_CLASS_FUNC //[啟動注冊類的全部TOLUA函數(shù)] BEGIN_TOLUA_FUN_MAP(Gk8HttpService) TOLUA_CLASS(Gk8HttpService,Gk8BaseObj,"[網絡類]","[HTTP服務類]") TOLUA_CLASS_FUNC(Gk8HttpService,Create,"[Gk8HttpService* Create(Gk8BaseObj* pSuperObj=NULL)") TOLUA_CLASS_FUNC(Gk8HttpService,SetHttpUrl,"[GK8_VOID SetHttpUrl(GK8_LPCSTR lpHttpUrl)]") TOLUA_CLASS_FUNC(Gk8HttpService,SetHeader,"[GK8_VOID SetHeader(GK8_LPCSTR lpUserId,GK8_LPCSTR lpUniqueKey)]") TOLUA_CLASS_FUNC(Gk8HttpService,SetUseZip,"[GK8_VOID SetUseZip(GK8_BOOL bUseZip)]") TOLUA_CLASS_FUNC(Gk8HttpService,SendMessage,"[GK8_VOID SendMessage(Gk8ByteMaker* pRequestData)]") TOLUA_CLASS_FUNC(Gk8HttpService,PutMessage,"[GK8_VOID PutMessage(Gk8ByteMaker* pRequestData)]") TOLUA_CLASS_FUNC(Gk8HttpService,SendCachedMessage,"[GK8_VOID SendCachedMessage()]") END_TOLUA_FUN_MAP /////////////////////////////////////////////CLASS-TOSPP//////////////////////////////////////////////////// BEGIN_TOSPP_MAP(Gk8HttpService,Gk8BaseObj) TOSPP_FUNC(Gk8HttpService,SetHttpUrl,' ',"s","SetHttpUrl(lpHttpUrl)") TOSPP_FUNC(Gk8HttpService,SetHeader,' ',"ss","SetHeader(lpUserId,lpUniqueKey)") TOSPP_FUNC(Gk8HttpService,SetUseZip,' ',"b","SetUseZip(bUseZip)") TOSPP_FUNC(Gk8HttpService,SendMessage,' ',"p","SendMessage(iRequestDataPtr)") TOSPP_FUNC(Gk8HttpService,PutMessage,' ',"p","PutMessage(iRequestDataPtr)") TOSPP_FUNC(Gk8HttpService,SendCachedMessage,' '," ","SendCachedMessage()") END_TOSPP_MAP() Gk8HttpService::Gk8HttpService() { Destroy(); } Gk8HttpService::~Gk8HttpService() { Destroy(); } Gk8HttpService* Gk8HttpService::Create(Gk8BaseObj* pSuperObj) { Gk8HttpService* pHttpService=new Gk8HttpService(); if(pSuperObj) pHttpService->SetSuper(pSuperObj); return pHttpService; } GK8_VOID Gk8HttpService::Destroy() { m_sHttpUrl=""; m_iRequestData.Destroy(); m_bUseZip=false; m_sUserId="0"; m_sUniqueKey="0"; } //[設置HTTP地址] GK8_VOID Gk8HttpService::SetHttpUrl(GK8_LPCSTR lpHttpUrl) { m_sHttpUrl=lpHttpUrl; } //[設置頭信息] GK8_VOID Gk8HttpService::SetHeader(GK8_LPCSTR lpUserId,GK8_LPCSTR lpUniqueKey) { m_sUserId=lpUserId; m_sUniqueKey=lpUniqueKey; } //[設置是否使用壓縮] GK8_VOID Gk8HttpService::SetUseZip(GK8_BOOL bUseZip) { m_bUseZip=bUseZip; } //[發(fā)送消息] GK8_VOID Gk8HttpService::SendMessage(Gk8ByteMaker* pRequestData) { pRequestData->ShiftTo(m_iRequestData); HttpRequestStarted(); } //[把二進制流重組成新的對象] GK8_VOID Gk8HttpService::PutMessage(Gk8ByteMaker* pRequestData) { pRequestData->ShiftTo(m_iRequestData); } GK8_VOID Gk8HttpService::SendCachedMessage() { HttpRequestStarted(); } //[開始HTTP請求] GK8_VOID Gk8HttpService::HttpRequestStarted() { //[全部消息前加上用戶ID和緩存唯一KEY] m_iRequestData.WriteStrAt(0,m_sUniqueKey); m_iRequestData.WriteStrAt(0,m_sUserId); Gk8HttpRequest* pHttpRequest=new Gk8HttpRequest(); pHttpRequest->SetRequestType(Gk8HttpRequest::kHttpPost); pHttpRequest->SetHttpUrl(m_sHttpUrl); pHttpRequest->SetResponseCallBack(this,(HTTPREQUEST_CALLBACK_FUN)&Gk8HttpService::HttpRequestCompleted); pHttpRequest->SetRequestData(&m_iRequestData); Gk8HttpClient::GetInstance()->Send(pHttpRequest); } //[HTTP請求完成二進制通信回調] GK8_VOID Gk8HttpService::HttpRequestCompleted(GK8_LPVOID lpHttpResponse) { Gk8HttpResponse* pHttpResponse=(Gk8HttpResponse*)lpHttpResponse; if(!pHttpResponse) return; if(!pHttpResponse->IsSucceed()) { _GK8ERR<<"Response Failed Error Is "<<pHttpResponse->GetErrorBuffer()<<CR; return; } Gk8ByteMaker* pResponseData=pHttpResponse->GetResponseData(); //[把服務器數(shù)據(jù)派遣到腳本中] Gk8Var iByteMakerVar(pResponseData,pResponseData->GetObjId()); #ifdef _GK8_SPP_ OnCall((GK8_LPCSTR)sg_ReceiveMessageFun,iByteMakerVar); #else RunEventWithArgs(sg_ReceiveMessageFun,iByteMakerVar); #endif }
以上就是如何使用HTTP實現(xiàn)二進制傳輸,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創(chuàng)新互聯(lián)-成都網站建設公司行業(yè)資訊頻道。
文章題目:如何使用HTTP實現(xiàn)二進制傳輸-創(chuàng)新互聯(lián)
網站網址:http://vcdvsql.cn/article38/csissp.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網站設計公司、全網營銷推廣、網頁設計公司、App開發(fā)、虛擬主機
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)