mirror of
https://github.com/Links2004/arduinoWebSockets.git
synced 2025-07-16 08:42:06 +02:00
add support for setting:
- Access-Control-Allow-Origin (#25) - Sec-WebSocket-Protocol () add _server->close(); for ESP
This commit is contained in:
@ -25,8 +25,11 @@
|
|||||||
#include "WebSockets.h"
|
#include "WebSockets.h"
|
||||||
#include "WebSocketsServer.h"
|
#include "WebSocketsServer.h"
|
||||||
|
|
||||||
WebSocketsServer::WebSocketsServer(uint16_t port) {
|
WebSocketsServer::WebSocketsServer(uint16_t port, String origin, String protocol) {
|
||||||
_port = port;
|
_port = port;
|
||||||
|
_origin = origin;
|
||||||
|
_protocol = protocol;
|
||||||
|
|
||||||
_server = new WEBSOCKETS_NETWORK_SERVER_CLASS(port);
|
_server = new WEBSOCKETS_NETWORK_SERVER_CLASS(port);
|
||||||
|
|
||||||
_cbEvent = NULL;
|
_cbEvent = NULL;
|
||||||
@ -37,7 +40,12 @@ WebSocketsServer::~WebSocketsServer() {
|
|||||||
// disconnect all clients
|
// disconnect all clients
|
||||||
disconnect();
|
disconnect();
|
||||||
|
|
||||||
|
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
|
||||||
|
_server->close();
|
||||||
|
#else
|
||||||
// TODO how to close server?
|
// TODO how to close server?
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -540,11 +548,21 @@ void WebSocketsServer::handleHeader(WSclient_t * client) {
|
|||||||
"Sec-WebSocket-Version: 13\r\n"
|
"Sec-WebSocket-Version: 13\r\n"
|
||||||
"Sec-WebSocket-Accept: ");
|
"Sec-WebSocket-Accept: ");
|
||||||
client->tcp->write(sKey.c_str(), sKey.length());
|
client->tcp->write(sKey.c_str(), sKey.length());
|
||||||
client->tcp->write("\r\n");
|
|
||||||
|
if(_origin.length() > 0) {
|
||||||
|
String origin = "\r\nAccess-Control-Allow-Origin: ";
|
||||||
|
origin += _origin;
|
||||||
|
origin += "\r\n";
|
||||||
|
client->tcp->write(origin.c_str(), origin.length());
|
||||||
|
}
|
||||||
|
|
||||||
if(client->cProtocol.length() > 0) {
|
if(client->cProtocol.length() > 0) {
|
||||||
// TODO add api to set Protocol of Server
|
String protocol = "\r\nSec-WebSocket-Protocol: ";
|
||||||
client->tcp->write("Sec-WebSocket-Protocol: arduino\r\n");
|
protocol += _protocol;
|
||||||
|
protocol += "\r\n";
|
||||||
|
client->tcp->write(protocol.c_str(), protocol.length());
|
||||||
|
} else {
|
||||||
|
client->tcp->write("\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// header end
|
// header end
|
||||||
|
@ -38,7 +38,7 @@ public:
|
|||||||
|
|
||||||
typedef std::function<void (uint8_t num, WStype_t type, uint8_t * payload, size_t length)> WebSocketServerEvent;
|
typedef std::function<void (uint8_t num, WStype_t type, uint8_t * payload, size_t length)> WebSocketServerEvent;
|
||||||
|
|
||||||
WebSocketsServer(uint16_t port);
|
WebSocketsServer(uint16_t port, String origin = "", String protocol = "arduino");
|
||||||
~WebSocketsServer(void);
|
~WebSocketsServer(void);
|
||||||
|
|
||||||
void begin(void);
|
void begin(void);
|
||||||
@ -74,6 +74,8 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
uint16_t _port;
|
uint16_t _port;
|
||||||
|
String _origin;
|
||||||
|
String _protocol;
|
||||||
|
|
||||||
WEBSOCKETS_NETWORK_SERVER_CLASS * _server;
|
WEBSOCKETS_NETWORK_SERVER_CLASS * _server;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user