Here's my problem: I have a NetConnection object connected to a server. On top I create a NetStream object and it started to play a file from the server. Classic so far.
What I need now, is to be able to send some (short) messages back to the server, at various moments during playtime but, clearly, using the existing TCP connection.
From what I can read in the docs, the underlying NetConnection object supports "two-way connection between a client and a server" and obviously the TCP layer supports it. I am aware of TCP networking concepts fairly well, but definitely not of how Flash implements them.
Is this correct? Can it be done using NetConnection (or some other mechanism)?
How would I go about doing this (an example would be great, but a conceptual description of the process would work just as well). How exactly do I send a message from the client to the server via NetConnection?
Does the active NetStream object interfere in any way with such an operation?
Thanks.
Yes, you can.
I assume, we are talking about connection to Flash media Server.
Use NetConnection.call() method which remotely executes server-side script method.
public function call(command:String, responder:Responder, ... arguments):void
You have to define this server-side method as a prototype of connection client class
e.g.
Client.prototype.MyMethod = function(arg)
{
trace("Server received " + arg + "\n");
}
Then calling this method should look like:
var nc:NetConnection;
//initialize net connection and connect
nc.call("MyMethod", null, "Hello, server");
If you need to get some result - use Responder class instance instead of null.
If you need server to call client's method use server-side "call" function on client object. In this case you'll have to define some object at client side, wich has the callback method:
Client side:
var obj = new Object();
obj.MyCallback = function(arg:Object)
{
trace ("Received message from server: " + arg as String);
}
nc.client = obj;
Server side:
clientObject.call("MyCallback", null, "Hello, client");
Regards, David.