Introduction
In this tutorial, you can learn the basics of how to communicate with the Furhat system by using a TCP socket by following a C Sharp example implementation. This example only exemplifies a one-way communication with Furhat. For more details about distributed systems communication with FurhatOS and IrisTK, consult Distributed Systems.
Note: this tutorial assumes that you:
- Have a Furhat robot
- Can access the robot through a web browser
- Have Visual Studio installed for running the example (Monodevelop can be used as an alternative)
Connect using the TCP broker
First, create a TCP Client that connects with FurhatOS. In this case, we are connecting to the system through the local dev-server and are thus using "localhost" as the hostname of our client. The default port for TCP communication with Furhat is 1932.
TcpClient socket = new TcpClient("localhost", 1932);
Send messages
Define a function that allows you to send messages over the TCP socket
public static void Send(string msg, TcpClient socket)
{
//Provides the underlying stream of data for network access
NetworkStream netStream = socket.GetStream();
//Converts the intended message to byte format
Byte[] sendBytes = Encoding.ASCII.GetBytes(msg);
//Sends the message over the network
netStream.Write(sendBytes, 0, sendBytes.Length);
}
Send a message that connects the socket to the Furhat broker.
Send("CONNECT furhat MyClassName\n", socket);
As an example, we will make the robot say the phrase "Hello there". For that, we will create two strings. One with an header for an action.speech event and one that will hold a deserialized JSON event object. For more information about what type of events you can send and receive from Furhat, please consult IrisTK Events.
string sendEventHeader = "EVENT action.speech ";
string sampleSayEvent =
"{\"class\" : \"iristk.system.Event\", " +
"\"event_name\" : \"action.speech\", " +
"\"event_id\" : \"my_unique_id_123\", " +
"\"text\" : \"Hello there\"}";
To finalize this example you should first send the header of the event and the number of bytes of the JSON event to FurhatOS.
Send(sendEventHeader + ASCIIEncoding.ASCII.GetByteCount(sampleSayEvent) + "\n", socket);
And send the actual event that makes Furhat say Hello there.
Send(sampleSayEvent, socket);
You can download the full C Sharp example which you can run as a console project in Visual Studio here.