-
Notifications
You must be signed in to change notification settings - Fork 17
Server
Go back to API Reference
A Websocket is a 2-way communication channel between 2 endpoints. In order to initiate a connection, Client handpoints connect to Server endpoints.
WebsocketsServer lets you listen for connections and accept clients.
To include WebsocketsServer:
#include <tiny_websockets/server.hpp>WebsocketsServer constructor takes an abstract network::TcpServer but it is created by default according to the platform you compile on.
So, in order to create a WebsocketsServer instance just use the deafult constructor:
websockets::WebsocketsServer server;
// use server, listen for connections ...
// ...In order to listen for connections, use the method listen.
void listen(uint16_t port);websockets::WebsocketsServer server;
server.listen(8080); // server will listen on port 8080In order to accept clients, use accept.
WebsocketsClient accept();accept will block until a client is connected. accept will return the newly connected WebsocketsClient. Now you can handle the client as normal.
If you wish to know more about the usage of WebsocketsClient, check out the Wiki Page
WebsocketsClient client = server.accept();
client.send("Hello New Client!");You can check if a server is still live and connected by using the method available.
bool available();A boolean value that indicates if the server is listening.
true: server is listening.
false: server has failed and is no longer listening.
You can check if a client is waiting to connect by using the method poll().
bool poll();A boolean value that indicates if there is a client waiting to connect.
true: there is a client waiting to connect.
false: there are no clients waiting to connect.
If poll() returns true, accept is guaranteed to not block.
#include <tiny_websockets/client.hpp>
#include <tiny_websockets/server.hpp>
#include <iostream>
using namespace websockets;
int main() {
WebsocketsServer server;
server.listen(8080);
while(server.available()) {
auto client = server.accept();
// Do something with the client
}
}/*
Simple Echo Server that can handle one client at a time.
After running this demo, there will be a websockets server running
on port SERVER_PORT (default 8080), that server will accept connections
and will respond with "echo" messages for every received message (one
clinet at a time, no multiple connections).
The code:
1. Sets up a server
2. Accepts a client
3. While the client connected, wait for a message
4. if that message is text, send an echo. otherwise don't do anything
5. When the client is no longer available, close the connection and start from step 1.
*/
#define SERVER_PORT 8080
#include <tiny_websockets/client.hpp>
#include <tiny_websockets/server.hpp>
#include <iostream>
using namespace websockets;
int main() {
WebsocketsServer server;
server.listen(SERVER_PORT);
// while the server is alive
while(server.available()) {
// accept a client
WebsocketsClient client = server.accept();
while(client.available()) {
// get a message, if it is text, return an echo
auto message = client.readBlocking();
if(message.isText()){
client.send("Echo: " + message.data());
}
}
// close the connection
client.close();
}
}/*
Single threaded Echo Server that can handle multiple clients.
After running this demo, there will be a websockets server running
on port SERVER_PORT (default 8080), that server will accept connections
and will respond with "echo" messages for every received message.
The code:
1. Sets up a server
2. While possible, checks if there is a client wanting to connect
2-1. If there is, accept the connection
2-2. Set up callbacks (for incoming messages)
2-3. Store the client in a collection for future use
3. For every client in the collection, poll for incoming events
When a message is received: respond to that message with `"Echo: " + message.data()`
*/
#define SERVER_PORT 8080
#include <tiny_websockets/client.hpp>
#include <tiny_websockets/server.hpp>
#include <string>
#include <iostream>
#include <vector>
using namespace websockets;
// a collection of all connected clients
std::vector<WebsocketsClient> allClients;
// this method goes thrugh every client and polls for new messages and events
void pollAllClients() {
for(auto& client : allClients) {
client.poll();
}
}
// this callback is common for all clients, the client that sent that
// message is the one that gets the echo response
void onMessage(WebsocketsClient& client, WebsocketsMessage message) {
std::cout << "Got Message: `" << message.data() << "`, Sending Echo." << std::endl;
client.send("Echo: " + message.data());
}
int main() {
WebsocketsServer server;
server.listen(SERVER_PORT);
// while the server is alive
while(server.available()) {
// if there is a client that wants to connect
if(server.poll()) {
//accept the connection and register callback
std::cout << "Accepting a new client!" << std::endl;
WebsocketsClient client = server.accept();
client.onMessage(onMessage);
// store it for later use
allClients.push_back(client);
}
// check for updates in all clients
pollAllClients();
}
}Go back to API Reference
Written by Gil Maimon @ 2019