Hello,
I am writing a short script which will get a response from a server, and need some advice.
The code I have so far is as follows:
using UnityEngine;
using System.Collections;
public class tcpSession : MonoBehaviour {
public void Request(string message, string returnMethod) {
// Open sockets session
StartCoroutine(socketRoutine(message, returnMethod));
}
IEnumerator socketRoutine(string packet, string returnMethod)
{
// Start TCP session, Send Packet, Wait Reply (or timeout), Fire returnMethod with response output. Close TCP session.
string output = "";
----->>> SOMETHING HERE...
// Fire returnMethod with output
Fire(returnMethod, output);
// Break the Coroutine...
yield break;
}
void Fire(string method, string value)
{
SendMessage(method, value);
}
}
It works by having another script call the Request Method, and providing a return method. The request is processed using a coroutine to keep the UI running smoothly. I would like to send the ‘packet’ / message to a TCP server, wait for reply (or timeout after 10 seconds), and then set the output which fires the message back to the returnMethod.
What’s the best way to send a TCP message, and wait for a reply?