Skip to content

Commit d2d06b5

Browse files
committed
Merge pull request Links2004#13 from jc19000/master
Update websocket client handshake
2 parents 39f912b + b60599d commit d2d06b5

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

src/WebSocketsClient.cpp

+17-25
Original file line numberDiff line numberDiff line change
@@ -272,33 +272,25 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
272272

273273
unsigned long start = micros();
274274

275-
//todo use tcp.write only once
276-
277-
client->tcp.write("GET ");
278-
client->tcp.write(client->cUrl.c_str(), client->cUrl.length());
279-
client->tcp.write(" HTTP/1.1\r\n"
280-
"Host: ");
281-
282-
client->tcp.write(_host.c_str(), _host.length());
283-
client->tcp.write("\r\n"
284-
"Upgrade: websocket\r\n"
285-
"Connection: Upgrade\r\n"
286-
"User-Agent: arduino-WebSocket-Client\r\n"
287-
"Sec-WebSocket-Version: 13\r\n"
288-
"Sec-WebSocket-Protocol: arduino\r\n" // todo add api to set Protocol of Server
289-
"Sec-WebSocket-Key: ");
290-
291-
client->tcp.write(client->cKey.c_str(), client->cKey.length());
292-
client->tcp.write("\r\n");
293-
294-
if(client->cExtensions.length() > 0) {
295-
client->tcp.write("Sec-WebSocket-Extensions: ");
296-
client->tcp.write(client->cExtensions.c_str(), client->cExtensions.length());
297-
client->tcp.write("\r\n");
275+
String handshake;
276+
handshake="GET "+client->cUrl+" HTTP/1.1\r\n";
277+
handshake+="Host: "+_host+"\r\n";
278+
handshake+="Upgrade: websocket\r\n";
279+
handshake+="Connection: Upgrade\r\n";
280+
handshake+="User-Agent: arduino-WebSocket-Client\r\n";
281+
handshake+="Sec-WebSocket-Version: 13\r\n";
282+
handshake+="Sec-WebSocket-Protocol: arduino\r\n";
283+
handshake+="Sec-WebSocket-Key: "+client->cKey+"\r\n";
284+
285+
if(client->cExtensions.length() > 0) {
286+
handshake+="Sec-WebSocket-Extensions: "+client->cExtensions+"\r\n";
298287
}
288+
289+
handshake+="\r\n";
290+
client->tcp.write(handshake.c_str(), handshake.length());
291+
292+
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] sending header... Done (%uus).\n", (micros() - start));
299293

300-
// Header end wait for Server response
301-
client->tcp.write("\r\n");
302294

303295
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] sending header... Done (%uus).\n", (micros() - start));
304296

0 commit comments

Comments
 (0)