2024-05-24 12:23:42 +08:00
|
|
|
#ifndef HV_WEBSOCKET_CHANNEL_H_
|
|
|
|
#define HV_WEBSOCKET_CHANNEL_H_
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
#include "Channel.h"
|
|
|
|
|
|
|
|
#include "wsdef.h"
|
|
|
|
#include "hmath.h"
|
|
|
|
|
|
|
|
namespace hv {
|
|
|
|
|
2024-05-24 12:29:09 +08:00
|
|
|
class HV_EXPORT WebSocketChannel : public SocketChannel {
|
2024-05-24 12:23:42 +08:00
|
|
|
public:
|
|
|
|
ws_session_type type;
|
|
|
|
WebSocketChannel(hio_t* io, ws_session_type type = WS_CLIENT)
|
|
|
|
: SocketChannel(io)
|
|
|
|
, type(type)
|
2024-05-24 12:29:09 +08:00
|
|
|
, opcode(WS_OPCODE_CLOSE)
|
2024-05-24 12:23:42 +08:00
|
|
|
{}
|
|
|
|
~WebSocketChannel() {}
|
|
|
|
|
|
|
|
// isConnected, send, close
|
|
|
|
|
|
|
|
int send(const std::string& msg, enum ws_opcode opcode = WS_OPCODE_TEXT, bool fin = true) {
|
|
|
|
return send(msg.c_str(), msg.size(), opcode, fin);
|
|
|
|
}
|
|
|
|
|
2024-05-24 12:29:09 +08:00
|
|
|
int send(const char* buf, int len, enum ws_opcode opcode = WS_OPCODE_BINARY, bool fin = true);
|
2024-05-24 12:23:42 +08:00
|
|
|
|
|
|
|
// websocket fragment
|
2024-05-24 12:29:09 +08:00
|
|
|
int send(const char* buf, int len, int fragment, enum ws_opcode opcode = WS_OPCODE_BINARY);
|
2024-05-24 12:23:42 +08:00
|
|
|
|
2024-05-24 12:29:09 +08:00
|
|
|
int sendPing();
|
|
|
|
int sendPong();
|
2024-05-24 12:23:42 +08:00
|
|
|
|
2024-05-24 12:29:09 +08:00
|
|
|
int close() {
|
|
|
|
return SocketChannel::close(type == WS_SERVER);
|
2024-05-24 12:23:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2024-05-24 12:29:09 +08:00
|
|
|
int sendFrame(const char* buf, int len, enum ws_opcode opcode = WS_OPCODE_BINARY, bool fin = true);
|
2024-05-24 12:23:42 +08:00
|
|
|
|
2024-05-24 12:29:09 +08:00
|
|
|
public:
|
|
|
|
enum ws_opcode opcode;
|
2024-05-24 12:23:42 +08:00
|
|
|
private:
|
|
|
|
Buffer sendbuf_;
|
|
|
|
std::mutex mutex_;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef std::shared_ptr<hv::WebSocketChannel> WebSocketChannelPtr;
|
|
|
|
|
|
|
|
#endif // HV_WEBSOCKET_CHANNEL_H_
|