Skip to content

Commit b07333c

Browse files
committed
WSockerServer add generic templated methods to send text/string/binary
variadic tpls would allow easy sending for text/strings contructables
1 parent cde857b commit b07333c

1 file changed

Lines changed: 96 additions & 38 deletions

File tree

src/AsyncWSocket.h

Lines changed: 96 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ class WSocketClient {
283283
inQfull, // inbound Q is full, won't receive new messages
284284
outQfull, // outboud Q is full, won't send new messages
285285
Qsfull, // both Qs are full
286-
disconnected // peer connection is broken
286+
disconnected, // peer connection is broken
287+
na // client is not available
287288
};
288289

289290
enum class overflow_t {
@@ -606,8 +607,10 @@ class WSocketServer : public AsyncWebHandler {
606607
* @param code
607608
* @param message
608609
*/
609-
void close(uint32_t id, uint16_t code = 1000, const char* message = NULL){
610-
if (WSocketClient* c = getClient(id)) c->close(code, message);
610+
WSocketClient::err_t close(uint32_t id, uint16_t code = 1000, const char* message = NULL){
611+
if (WSocketClient* c = getClient(id))
612+
return c->close(code, message);
613+
else return WSocketClient::err_t::na;
611614
}
612615

613616
/**
@@ -630,8 +633,7 @@ class WSocketServer : public AsyncWebHandler {
630633
WSocketClient::err_t ping(uint32_t id, const char* data = NULL, size_t len = 0){
631634
if (WSocketClient *c = getClient(id))
632635
return c->ping(data, len);
633-
else
634-
return WSocketClient::err_t::disconnected;
636+
else return WSocketClient::err_t::na;
635637
}
636638

637639
/**
@@ -653,47 +655,103 @@ class WSocketServer : public AsyncWebHandler {
653655
WSocketClient::err_t message(uint32_t id, WSMessagePtr m);
654656

655657
/**
656-
* @brief send genric message to all available clients
658+
* @brief send generic message to all available clients
657659
*
658660
* @param m
659661
* @return msgall_err_t
660662
*/
661663
msgall_err_t messageAll(WSMessagePtr m);
662664

665+
/**
666+
* @brief Send text message to client
667+
* this template can accept anything that std::string can be made of
668+
*
669+
* @tparam Args
670+
* @param id
671+
* @param args
672+
* @return WSocketClient::err_t
673+
*/
674+
template<typename... Args>
675+
WSocketClient::err_t text(uint32_t id, Args&&... args){
676+
if (hasClient(id))
677+
return message(id, std::make_shared<WSMessageContainer<std::string>>(WSFrameType_t::text, true, std::forward<Args>(args)...));
678+
else return WSocketClient::err_t::na;
679+
}
680+
681+
/**
682+
* @brief Send text message to all avalable clients
683+
* this template can accept anything that std::string can be made of
684+
*
685+
* @tparam Args
686+
* @param args
687+
* @return WSocketClient::err_t
688+
*/
689+
template<typename... Args>
690+
msgall_err_t textAll(uint32_t id, Args&&... args){
691+
return messageAll(std::make_shared<WSMessageContainer<std::string>>(WSFrameType_t::text, true, std::forward<Args>(args)...));
692+
}
693+
694+
/**
695+
* @brief Send String text message to client
696+
* this template can accept anything that Arduino String can be made of
697+
*
698+
* @tparam Args Arduino String constructor arguments
699+
* @param id clien id to send message to
700+
* @param args
701+
* @return WSocketClient::err_t
702+
*/
703+
template<typename... Args>
704+
WSocketClient::err_t string(uint32_t id, Args&&... args){
705+
if (hasClient(id))
706+
return message(std::make_shared<WSMessageContainer<String>>(WSFrameType_t::text, true, std::forward<Args>(args)...));
707+
else return WSocketClient::err_t::na;
708+
}
663709

664-
/*
665-
bool text(uint32_t id, const uint8_t *message, size_t len);
666-
bool text(uint32_t id, const char *message, size_t len);
667-
bool text(uint32_t id, const char *message);
668-
bool text(uint32_t id, const String &message);
669-
bool text(uint32_t id, AsyncWebSocketMessageBuffer *buffer);
670-
bool text(uint32_t id, AsyncWebSocketSharedBuffer buffer);
671-
672-
enqueue_err_t textAll(const uint8_t *message, size_t len);
673-
enqueue_err_t textAll(const char *message, size_t len);
674-
enqueue_err_t textAll(const char *message);
675-
enqueue_err_t textAll(const String &message);
676-
enqueue_err_t textAll(AsyncWebSocketMessageBuffer *buffer);
677-
enqueue_err_t textAll(AsyncWebSocketSharedBuffer buffer);
678-
679-
bool binary(uint32_t id, const uint8_t *message, size_t len);
680-
bool binary(uint32_t id, const char *message, size_t len);
681-
bool binary(uint32_t id, const char *message);
682-
bool binary(uint32_t id, const String &message);
683-
bool binary(uint32_t id, AsyncWebSocketMessageBuffer *buffer);
684-
bool binary(uint32_t id, AsyncWebSocketSharedBuffer buffer);
685-
686-
enqueue_err_t binaryAll(const uint8_t *message, size_t len);
687-
enqueue_err_t binaryAll(const char *message, size_t len);
688-
enqueue_err_t binaryAll(const char *message);
689-
enqueue_err_t binaryAll(const String &message);
690-
enqueue_err_t binaryAll(AsyncWebSocketMessageBuffer *buffer);
691-
enqueue_err_t binaryAll(AsyncWebSocketSharedBuffer buffer);
692-
693-
size_t printf(uint32_t id, const char *format, ...) __attribute__((format(printf, 3, 4)));
694-
size_t printfAll(const char *format, ...) __attribute__((format(printf, 2, 3)));
695-
*/
710+
/**
711+
* @brief Send String text message all avalable clients
712+
* this template can accept anything that Arduino String can be made of
713+
*
714+
* @tparam Args
715+
* @param id client id to send message to
716+
* @param args Arduino String constructor arguments
717+
* @return WSocketClient::err_t
718+
*/
719+
template<typename... Args>
720+
msgall_err_t stringAll(uint32_t id, Args&&... args){
721+
return messageAll(std::make_shared<WSMessageContainer<String>>(WSFrameType_t::text, true, std::forward<Args>(args)...));
722+
}
723+
724+
/**
725+
* @brief Send binary message to client
726+
* this template can accept anything that std::vector can be made of
727+
*
728+
* @tparam Args
729+
* @param id client id to send message to
730+
* @param args std::vector constructor arguments
731+
* @return WSocketClient::err_t
732+
*/
733+
template<typename... Args>
734+
WSocketClient::err_t binary(uint32_t id, Args&&... args){
735+
if (hasClient(id))
736+
return message(id, std::make_shared< WSMessageContainer<std::vector<uint8_t>> >(WSFrameType_t::binary, true, std::forward<Args>(args)...));
737+
else return WSocketClient::err_t::na;
738+
}
739+
740+
/**
741+
* @brief Send binary message all avalable clients
742+
* this template can accept anything that std::vector can be made of
743+
*
744+
* @tparam Args
745+
* @param id client id to send message to
746+
* @param args std::vector constructor arguments
747+
* @return WSocketClient::err_t
748+
*/
749+
template<typename... Args>
750+
msgall_err_t binaryAll(uint32_t id, Args&&... args){
751+
return messageAll(std::make_shared< WSMessageContainer<std::vector<uint8_t>> >(WSFrameType_t::binary, true, std::forward<Args>(args)...));
752+
}
696753

754+
// set webhanshake handler
697755
void handleHandshake(AwsHandshakeHandler handler) {
698756
_handshakeHandler = handler;
699757
}

0 commit comments

Comments
 (0)