This repository has been archived on 2021-11-25. You can view files and clone it, but cannot push or open issues/pull-requests.
KChatWSServer/websocket.h

40 lines
763 B
C++

#pragma once
#include <string>
#include "gsock.h"
int HandleKey(sock& s, const std::string& key);
int Handshake(sock& s);
struct WSFrame
{
// 1 bit
bool fin;
// 1 bit
bool rsv1, rsv2, rsv3;
// 0x0 附加数据帧
// 0x1 文本数据帧
// 0x2 二进制数据帧
// 0x3~7 保留
// 0x8 连接关闭
// 0x9 ping
// 0xA pong
// 0xB~F 保留
// 4 bit
int opcode;
// 1 bit
bool ismask;
unsigned long long len;
// 4 byte
char mask[4];
// data是未经过编码处理的数据(如果源数据是带掩码的则已经经过解掩码处理)
std::string data;
};
int ReadFrame(sock& s, WSFrame& f);
int SendFrame(sock& s, const WSFrame& f);
int SendPong(sock& s, const WSFrame& ping);
int ReadMsg(sock& s, std::string& out_data);
int SendMsg(sock& s, const std::string& data, bool is_text = true);