我正在使用winsock2在websocket服務器上(使用C++)。眼下,I'm響應初始握手從客戶端(瀏覽器在我的情況),然後I'm從客戶端發送的數據:WebSocket,解碼數據幀(C++)
socket.send("Hello!");
我正嘗試將數據幀,但I'm解碼有幾個問題。讓我們來看看代碼:
int GetBit(const char * data, unsigned int idx)
{
unsigned int arIdx = idx/4;
unsigned int biIdx = idx % 4;
return (data[arIdx] >> biIdx) & 1;
}
void ParseDataFrame(const char * packet)
{
int FIN = GetBit(packet, 0);
unsigned char OPC = 0;
{
int opc0 = GetBit(packet, 4);
int opc1 = GetBit(packet, 5);
int opc2 = GetBit(packet, 6);
int opc3 = GetBit(packet, 7);
OPC |= (opc0 << 0);
OPC |= (opc1 << 1);
OPC |= (opc2 << 2);
OPC |= (opc3 << 3);
}
int MASK = GetBit(packet, 5);
}
I'm越來越:
FIN = 1
OPC = x6 (can´t be)
MAKS = 0 (can´t be)
從來就一直在讀通過WS協議,也許這個問題是在我的代碼。 在此先感謝!
編輯
我想提一提的是,連接正確建立,因爲在控制檯(鉻)沒有錯誤和socket.onopen事件被調用。
能否請您給我們至少前8個項目的內容在'爲const char * packet',然後告訴我們你預期在'OPC'和'MASK'中看到 – KompjoeFriek
MASK應該是1(屏蔽)和OPC x1(文本)。前8個字符:「-127,-122,-85,-60,-77,-32,-29,-95」。 (用std :: cout <<(int)packet [i] << std :: endl'打印它們) – Nacho