8000 add support for setting: · coderGods/arduinoWebSockets@ece771a · GitHub
[go: up one dir, main page]

Skip to content

Commit ece771a

Browse files
committed
add support for setting:
- Access-Control-Allow-Origin (Links2004#25) - Sec-WebSocket-Protocol () add _server->close(); for ESP
1 parent cf7652a commit ece771a

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/WebSocketsServer.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
#include "WebSockets.h"
2626
#include "WebSocketsServer.h"
2727

28-
WebSocketsServer::WebSocketsServer(uint16_t port) {
28+
WebSocketsServer::WebSocketsServer(uint16_t port, String origin, String protocol) {
2929
_port = port;
30+
_origin = origin;
31+
_protocol = protocol;
32+
3033
_server = new WEBSOCKETS_NETWORK_SERVER_CLASS(port);
3134

3235
_cbEvent = NULL;
@@ -37,7 +40,12 @@ WebSocketsServer::~WebSocketsServer() {
3740
// disconnect all clients
3841
disconnect();
3942

43+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
44+
_server->close();
45+
#else
4046
// TODO how to close server?
47+
#endif
48+
4149
}
4250

4351
/**
@@ -540,11 +548,21 @@ void WebSocketsServer::handleHeader(WSclient_t * client) {
540548
"Sec-WebSocket-Version: 13\r\n"
541549
"Sec-WebSocket-Accept: ");
542550
client->tcp->write(sKey.c_str(), sKey.length());
543-
client->tcp->write("\r\n");
551+
552+
if(_origin.length() > 0) {
553+
String origin = "\r\nAccess-Control-Allow-Origin: ";
554+
origin += _origin;
555+
origin += "\r\n";
556+
client->tcp->write(origin.c_str(), origin.length());
557+
}
544558

545559
if(client->cProtocol.length() > 0) {
546-
// TODO add api to set Protocol of Server
547-
client->tcp->write("Sec-WebSocket-Protocol: arduino\r\n");
560+
String protocol = "\r\nSec-WebSocket-Protocol: ";
561+
protocol += _protocol;
562+
protocol += "\r\n";
563+
client->tcp->write(protocol.c_str(), protocol.length());
564+
} else {
565+
client->tcp->write("\r\n");
548566
}
549567

550568
// header end

src/WebSocketsServer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class WebSocketsServer: private WebSockets {
3838

3939
typedef std::function<void (uint8_t num, WStype_t type, uint8_t * payload, size_t length)> WebSocketServerEvent;
4040

41-
WebSocketsServer(uint16_t port);
41+
WebSocketsServer(uint16_t port, String origin = "", String protocol = "arduino");
4242
~WebSocketsServer(void);
4343

4444
void begin(void);
@@ -74,6 +74,8 @@ class WebSocketsServer: private WebSockets {
7474

7575
protected:
7676
uint16_t _port;
77+
String _origin;
78+
String _protocol;
7779

7880
WEBSOCKETS_NETWORK_SERVER_CLASS * _server;
7981

0 commit comments

Comments
 (0)
0