ImFusion SDK 4.3
NetworkCommunication.cpp
#include <ImFusion/Core/ByteBuffer.h>
#include <ImFusion/GL/SharedImageSet.h>
#include <ImFusion/Stream/Network/WebsocketClient.h>
#include <ImFusion/Stream/Network/WebsocketServer.h>
#include <ImFusion/Stream/Serialization.h>
#include <iostream>
using namespace ImFusion;
int main()
{
// This example demonstrates a simple WebSocket server and client using the Network library.
// It shows how to set up a server that listens for incoming connections and a client that connects to the server.
// The server echoes back any messages it receives from the client and prints them to the console.
// Normally, you would only have either a server or a client in your application, but for demonstration purposes, both are included here.
// Create and start a WebSocket server listening on localhost (127.0.0.1) at port 12345.
Network::WebsocketServer<> websocketServer{"127.0.0.1", 12345};
websocketServer.run();
// Set up a handler for when the server receives a new message.
websocketServer.signalNewMessage.connect([&websocketServer](std::shared_ptr<std::string> msg) {
std::cout << "Server received message: " << *msg << std::endl;
// Echo the received message back to the client.
websocketServer.sendMessage(*msg);
});
// Create a WebSocket client that will connect to the server at localhost:12345.
Network::WebsocketClient<> websocketClient{"127.0.0.1", 12345};
// Set up a handler for when the client receives a new message from the server.
websocketClient.signalNewMessage.connect([](std::shared_ptr<std::string> msg) {
std::cout << "Client received message back: " << *msg << std::endl;
});
// Connect the client to the server, with a timeout of 10 seconds (10000 ms).
websocketClient.connect(10000);
// Send a message from the client to the server. The server replies the same message.
websocketClient.sendMessage("Hello world!");
// ... (other application logic can go here)
// At the end, disconnect the client and stop the server to clean up resources.
websocketClient.disconnect();
websocketServer.stop();
// Serialize any data for sending.
SharedImageSet imageToSend; // Replace with your data
DataList dataToSend{&imageToSend};
if (sendBuffer)
{
// E.g. send buffer via the network
}
// ...
// Deserialize byte buffer on receiver site.
ByteBufferView receiveByteBuffer; // replace with your received serialized data
if (receivedData)
{
auto receivedImage = receivedData->extractFirst<SharedImageSet>();
// ...
}
}
Const view onto a sized buffer of raw binary data.
Definition ByteBuffer.h:36
Container for any number of Data instances such as image or meshes.
Definition DataList.h:30
ProtectedSignal< std::shared_ptr< Payload > > signalNewMessage
Signal emitted when a new message is received.
Definition ConnectionBase.h:41
WebSocket client implementation providing bidirectional communication.
Definition WebsocketClient.h:23
Websocket server implementation providing bidirectional communication.
Definition WebsocketServer.h:26
bool run() override
Starts listening for incoming connections.
Set of images independent of their storage location.
Definition SharedImageSet.h:42
T endl(T... args)
std::optional< ByteBuffer > dataToByteBuffer(const DataList &data)
Serializes a DataList into a ByteBuffer for streaming and data transfer over a network.
std::optional< OwningDataList > dataFromByteBuffer(const ByteBufferView &buffer)
Deserializes a ByteBuffer with content generated via dataToByteBuffer() back to an OwningDataList.
Namespace of the ImFusion SDK.
Definition Assert.h:7
Search Tab / S to search, Esc to close