forked from benkuper/juce_simpleweb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleWebSocketClient.h
More file actions
114 lines (83 loc) · 3.33 KB
/
SimpleWebSocketClient.h
File metadata and controls
114 lines (83 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
==============================================================================
juce_SimpleWebSocket.h
Created: 17 Jun 2020 11:22:54pm
Author: bkupe
==============================================================================
*/
#pragma once
#define USE_STANDALONE_ASIO 1
#define NOGDI
#define ASIO_DISABLE_SERIAL_PORT 1
using WsClient = SimpleWeb::SocketClient<SimpleWeb::WS>;
class SimpleWebSocketClientBase :
public juce::Thread
{
public:
SimpleWebSocketClientBase();
virtual ~SimpleWebSocketClientBase();
juce::String serverPath;
bool isConnected;
bool isClosing;
virtual void start(const juce::String& _serverPath);
virtual void send(const juce::String& message) {}
virtual void send(const char* data, int numData) {}
void send(const juce::MemoryBlock& data);
void stop();
virtual void stopInternal() {}
virtual void run();
virtual void initWS() {}
void handleNewConnectionCallback();
void handleConnectionClosedCallback(int status, const juce::String& reason);
void handleErrorCallback(const juce::String& message);
class Listener
{
public:
virtual ~Listener() {}
virtual void connectionOpened() {}
virtual void messageReceived(const juce::String& message) {}
virtual void dataReceived(const juce::MemoryBlock& data) {}
virtual void connectionClosed(int status, const juce::String& reason) {}
virtual void connectionError(const juce::String& message) {}
};
juce::ListenerList<Listener> webSocketListeners;
void addWebSocketListener(Listener* newListener) { webSocketListeners.add(newListener); }
void removeWebSocketListener(Listener* listener) { webSocketListeners.remove(listener); }
};
class SimpleWebSocketClient :
public SimpleWebSocketClientBase
{
public:
std::unique_ptr<WsClient> ws;
std::shared_ptr<WsClient::Connection> connection;
SimpleWebSocketClient();
~SimpleWebSocketClient();
void send(const juce::String& message) override;
void send(const char* data, int numData) override;
void stopInternal() override;
void initWS() override;
void onMessageCallback(std::shared_ptr<WsClient::Connection> connection, std::shared_ptr<WsClient::InMessage> in_message);
void onNewConnectionCallback(std::shared_ptr<WsClient::Connection> _connection);
void onConnectionCloseCallback(std::shared_ptr<WsClient::Connection> /*_connection*/, int status, const std::string& reason);
void onErrorCallback(std::shared_ptr<WsClient::Connection> /*_connection*/, const SimpleWeb::error_code& ec);
};
#if SIMPLEWEB_SECURE_SUPPORTED
using WssClient = SimpleWeb::SocketClient<SimpleWeb::WSS>;
class SecureWebSocketClient :
public SimpleWebSocketClientBase
{
public:
std::unique_ptr<WssClient> ws;
std::shared_ptr<WssClient::Connection> connection;
SecureWebSocketClient();
~SecureWebSocketClient();
void send(const juce::String& message) override;
void send(const char* data, int numData) override;
void stopInternal() override;
void initWS() override;
void onMessageCallback(std::shared_ptr<WssClient::Connection> connection, std::shared_ptr<WssClient::InMessage> in_message);
void onNewConnectionCallback(std::shared_ptr<WssClient::Connection> _connection);
void onConnectionCloseCallback(std::shared_ptr<WssClient::Connection> /*_connection*/, int status, const std::string& reason);
void onErrorCallback(std::shared_ptr<WssClient::Connection> /*_connection*/, const SimpleWeb::error_code& ec);
};
#endif