package com.imagetrend.net {
import flash.net.*;
import flash.errors.*;
import flash.events.*;
import flash.util.*;
public class SocketClient {
private var socket:Socket
;
public function SocketClient(host:String, port:int/*, Log log*/) {
try {
socket = new
Socket(host, port);
//Set function to catch reply
socket.addEventListener("socketData", getMessage);
//Set function to catch errors
socket.addEventListener("ioError", getIOError);
//trace("SocketClient.
socket : "+socket);
} catch ( x:IOError ) {
trace("SocketClient.error: "+ x);
}
}
public function chat(request:String) {
try {
//send message
sendMessage(request);
} catch (e:IOError) {
trace("Client.chat.exception
: "+e);
} //catch (InterruptedException e) {
//e.printStackTrace();
//}
//return resp;
}
public function sendMessage(msg:String) {
socket.writeUTF(msg);
socket.flush();
trace("Client.sendMessage(" + msg);
}
public function getIOError(evtObj:ErrorEvent) {
trace("
Client.IOError: "+evtObj.type);
for (var c:Object in evtObj) {
trace("evtObj["+c+"]: "+evtObj[c]);
}
}
public function getMessage(evtObj:ProgressEvent) {
//trace("getMessage.evtObj.type: "+evtObj.type);
//for (var c in evtObj) {
// trace("evtObj["+c+"]: "+evtObj[c]);
//}
var resp:String =
socket.readUTF ();
trace("Client received: "+resp);
}
public function disconnect():Void { //throws IOError {
trace("Client.disonnect()");
chat("GOODBYE");
socket.close();
}
}
}
Hi there,
I have a fairly simple idea and need help:
I want to build a simple socket connection (not XMLSocket!) to a server to exchange simple text based commands. The Flash Client sends a text command, the server answers with a response (text based). While this is very easy in e.g. C# I run into "threading" issues with Flash. Somehow it seems impossible to wait for the server response.
Why? Obviously the entire scenario is asynchronously. I have extended the Socket class and added a function for the data receive event:
aDispatcher.addEventListener(ProgressEventType.SOCKET_DATA, onDataReceived);
In the onDataReceived() I read all incoming data (via readUTFBytes(_length)) parse it and put it into a responseQueue (an Array).
Now comes the difficult part in question here:
I want to create a function SendCommand(cmd:String):String which takes a Command string as a parameter and returns the result.
So basically the function should look somewhat like this:
function SendCommand(cmd:String):String
{
writeUTFBytes(cmd);
var result:String = undefined;
// code to wait for result and assign it
return result;
}
In another area of the application I want to call it like this:
response = SendCommand("login name");
Alert.show(response),
Here is the problem:
SendCommand() is called, sends the data and immediately returns.
The returned response is undefined. Alert shows "undefined".
THEN the dataReceive event fires. Response gets processed.
However, there seems to be no way of waiting (idle) right after the writeUTFBytes call.
I have tried everything. If I do a long lasting ro loop, it seems as if Flash handles it single threaed, so the OnReceiveData Event gets fired immediatley after my idle for loop finishes.
Any idea?
Ralf
--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com
YAHOO! GROUPS LINKS
- Visit your group "flexcoders" on the web.
- To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
- Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com
YAHOO! GROUPS LINKS
- Visit your group "flexcoders" on the web.
- To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
- Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.

