Skip to content

Commit 4a9ef26

Browse files
committed
send the handshake response as a single TCP packet - splitting it causes Chrome to fail with a protocol error (unknown opcode 13.)
1 parent 3b67721 commit 4a9ef26

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

firmware/WebSocketsServer.cpp

+23-18
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,14 @@ bool WebSocketsServer::newClient(WEBSOCKETS_NETWORK_CLASS * TCPclient) {
346346

347347
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
348348
client->isSSL = false;
349-
client->tcp->setNoDelay(true);
349+
client->tcp->setNoDelay (true);
350350
#endif
351-
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
351+
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC) && ((WEBSOCKETS_NETWORK_TYPE != NETWORK_PARTICLE))
352352
// set Timeout for readBytesUntil and readStringUntil
353353
client->tcp->setTimeout(WEBSOCKETS_TCP_TIMEOUT);
354354
#endif
355355
client->status = WSC_HEADER;
356-
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
356+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC || WEBSOCKETS_NETWORK_TYPE == NETWORK_PARTICLE)
357357
IPAddress ip = client->tcp->remoteIP();
358358
DEBUG_WEBSOCKETS("[WS-Server][%d] new client from %d.%d.%d.%d\n", client->num, ip[0], ip[1], ip[2], ip[3]);
359359
#else
@@ -679,32 +679,37 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
679679

680680
client->status = WSC_CONNECTED;
681681

682-
client->tcp->write("HTTP/1.1 101 Switching Protocols\r\n"
682+
String response;
683+
response.reserve(256);
684+
685+
const char* prefix = "HTTP/1.1 101 Switching Protocols\r\n"
683686
"Server: arduino-WebSocketsServer\r\n"
684-
"Upgrade: websocket\r\n"
685687
"Connection: Upgrade\r\n"
686688
"Sec-WebSocket-Version: 13\r\n"
687-
"Sec-WebSocket-Accept: ");
688-
client->tcp->write((const uint8_t*)sKey.c_str(), sKey.length());
689+
"Sec-WebSocket-Accept: ";
690+
response += prefix;
691+
response += sKey;
692+
response += "\r\n";
689693

690694
if(_origin.length() > 0) {
691-
String origin = "\r\nAccess-Control-Allow-Origin: ";
692-
origin += _origin;
693-
origin += "\r\n";
694-
client->tcp->write((const uint8_t*)origin.c_str(), origin.length());
695+
response += "Access-Control-Allow-Origin: ";
696+
response += _origin;
697+
response += "\r\n";
695698
}
696699

697700
if(client->cProtocol.length() > 0) {
698-
String protocol = "\r\nSec-WebSocket-Protocol: ";
699-
protocol += _protocol;
700-
protocol += "\r\n";
701-
client->tcp->write((const uint8_t*)protocol.c_str(), protocol.length());
702-
} else {
703-
client->tcp->write("\r\n");
701+
response += "Sec-WebSocket-Protocol: ";
702+
response += _protocol;
703+
response += "\r\n";
704704
}
705705

706+
response += "Upgrade: websocket\r\n";
707+
708+
706709
// header end
707-
client->tcp->write("\r\n");
710+
response += ("\r\n");
711+
712+
client->tcp->write((const uint8_t*)response.c_str(), response.length());
708713

709714
headerDone(client);
710715

0 commit comments

Comments
 (0)