views:

48

answers:

1

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.

  1. Is this correct? Can it be done using NetConnection (or some other mechanism)?

  2. 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?

  3. Does the active NetStream object interfere in any way with such an operation?

Thanks.

+1  A: 

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.

David Goshadze
Actually, I won't be using Flash Media Server, I suppose I should have been more explicit, but if the Flash client supports the call, I think the server can be any AMF implementation (e.g. AMFPHP). Right?
Valeriu Paloş
I guess you are right. At the client side everything sould look the same. At server side implementation can vary. (Red 5 server for instance)
David Goshadze