@@ -233,18 +233,18 @@ bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
233233 // header has be added to payload
234234 // payload is forced to reserved 14 Byte but we may not need all based on the length and mask settings
235235 // offset in payload is calculatetd 14 - headerSize
236- if (client-> tcp -> write (&payloadPtr[(WEBSOCKETS_MAX_HEADER_SIZE - headerSize)], (length + headerSize)) != (length + headerSize)) {
236+ if (write (client, &payloadPtr[(WEBSOCKETS_MAX_HEADER_SIZE - headerSize)], (length + headerSize)) != (length + headerSize)) {
237237 ret = false ;
238238 }
239239 } else {
240240 // send header
241- if (client-> tcp -> write (&buffer[0 ], headerSize) != headerSize) {
241+ if (write (client, &buffer[0 ], headerSize) != headerSize) {
242242 ret = false ;
243243 }
244244
245245 if (payloadPtr && length > 0 ) {
246246 // send payload
247- if (client-> tcp -> write (&payloadPtr[0 ], length) != length) {
247+ if (write (client, &payloadPtr[0 ], length) != length) {
248248 ret = false ;
249249 }
250250 }
@@ -593,3 +593,56 @@ bool WebSockets::readCb(WSclient_t * client, uint8_t * out, size_t n, WSreadWait
593593#endif
594594 return true ;
595595}
596+
597+ /* *
598+ * write x byte to tcp or get timeout
599+ * @param client WSclient_t *
600+ * @param out uint8_t * data buffer
601+ * @param n size_t byte count
602+ * @return true if ok
603+ */
604+ size_t WebSockets::write (WSclient_t * client, uint8_t *out, size_t n) {
605+ if (out == NULL ) return 0 ;
606+ if (client == NULL ) return 0 ;
607+ unsigned long t = millis ();
608+ size_t len = 0 ;
609+ size_t total = 0 ;
610+ DEBUG_WEBSOCKETS (" [size_t] n: %d t: %d\n " , n, t);
611+ while (n > 0 ) {
612+ if (client->tcp == NULL ) {
613+ DEBUG_WEBSOCKETS (" [write] tcp is null!\n " );
614+ break ;
615+ }
616+
617+ if (!client->tcp ->connected ()) {
618+ DEBUG_WEBSOCKETS (" [write] not connected!\n " );
619+ break ;
620+ }
621+
622+ if ((millis () - t) > WEBSOCKETS_TCP_TIMEOUT) {
623+ DEBUG_WEBSOCKETS (" [write] write TIMEOUT! %d\n " , (millis () - t));
624+ break ;
625+ }
626+
627+ len = client->tcp ->write (out, n);
628+ if (len) {
629+ t = millis ();
630+ out += len;
631+ n -= len;
632+ total += len;
633+ // DEBUG_WEBSOCKETS("write %d left %d!\n", len, n);
634+ } else {
635+ // DEBUG_WEBSOCKETS("write %d failed left %d!\n", len, n);
636+ }
637+ #if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
638+ delay (0 );
639+ #endif
640+ }
641+ return total;
642+ }
643+
644+ size_t WebSockets::write (WSclient_t * client, const char *out) {
645+ if (client == NULL ) return 0 ;
646+ if (out == NULL ) return 0 ;
647+ return write (client, (uint8_t *)out, strlen (out));
648+ }
0 commit comments