After some kind of request it might be some time to explain detailed how I used the Socket in my IRC client. First of all I will explain how to bypass the securitymodel. I wrote about this before but I think it might be usefull to have it here again.
First of all Flash does not allow you to connect to servers that are outside the so-called sandbox of your Flash application. You are of course able to extend that sandbox by using Security.loadPolicyFile( ‘http://another-server/crossdomain.xml’ ); for example. Now most servers do not have such a file only for your pleasure.
The “simple” workaround here is using a proxy which is located on your server. A proxy is something that runs on your shell and is able to forward connections to other servers. Normally a connection goes direct from client to server (Client -> Server). Using a proxy would result in Client -> Proxy -> Server. Now the proxy is located on your server. That means every user that connects to some server would use your ip which is not good. Therefore I have to say this is not a satisfying workaround but at least you can connect to any server you want.
Proxys for IRC and other applications can be found at Google. An easy proxy is Proxy which is not hard to configure. For FTP applications I would suggest you using TLSWrap.
Now having the socket of your choice installed on your shell you are ready to go. IRC and many other protocols end every command with the CRLF. The binary representation of a CRLF is 13 10. I wrote some extended Socket for IRC but this might also be usefull for FTP. You can grab the sourcecode here. It is not very hard to understand. The sendString() method accepts a String which is different from the writeUTF8Bytes function of the socket. Most protocols do not like UTF-8 and so all the characters like äöü etc. would look scary on other clients. The sendString() function puts all characters into one ByteArray and terminates the string with a CRLF. After that the command will be send using writeBytes() and flush().
Now here is a short passage of code how I connect to other an IRC server. It is of course not the full application. I will release the whole thing when flIRC is finished and the code clean and smooth.
[as]
import flash.system.Security;
import flash.events.*;
import irc.*;
private var sock: IRCSocket;
private function main(): Void
{
Security.loadPolicyFile( ‘http://proxyhost/crossdomain.xml’ );
Security.allowDomain( ‘proxyhost’ );
Security.allowDomain( ‘proxyhost:port’ );
sock = new IRCSocket();
}
private function connect(): Void
{
if ( sock.connected )
{
sock.sendString( ‘QUIT :flIRC – http://blog.je2050.de/’ );
disconnect();
return;
}
registered = false;
// this is for dynamic connection
//sock.connect( host, uint( port ));
// we use a proxy
sock.connect( ‘proxyhost’, proxyport );
sock.onSockOpen = onSockOpen;
sock.onSockRead = onSockRead;
sock.onSockError = onSockError;
}
private function onSockOpen( event: Event ): Void
{
// … do the IRC logon process here
}
private function onSockError( event: Event )
{
// this is very simple for the moment
Alert.show( ‘Unknown socket error’, ‘Error’ );
}
private function onSockRead( input: String ): Void
{
if ( input == ” ) return;
// this is a simple way to get the informations you want
var tokens: Array = input.split( ‘ ‘ );
if ( tokens[ 0 ] == ‘PING’ )
{
// you are registered on an irc server after the first ping event
// registered = true;
// just show we got the event in the status window
//addStatusMessage( ‘Ping? Pong!’ );
// send the id back to the server
sock.sendString( ‘PONG ‘ + tokens[ 1 ] );
return;
}
// … handle all other events here
}
[/as]
I hope you get the idea. It is not very hard to establish a connection. I also hope this was interesting and usefull for you. Do not be shy and post a comment if I was not able to make this plain to you.
Links:
After some kind of request it might be some time to explain detailed how I used the Socket in my IRC client. First of all I will explain how to bypass the securitymodel. I wrote about this before but I think it might be usefull to have it here again.
First of all Flash does not allow you to connect to servers that are outside the so-called sandbox of your Flash application. You are of course able to extend that sandbox by using Security.loadPolicyFile( 'http://another-server/crossdomain.xml' ); for example. Now most servers do not have such a file only for your pleasure.
The "simple" workaround here is using a proxy which is located on your server. A proxy is something that runs on your shell and is able to forward connections to other servers. Normally a connection goes direct from client to server (Client -> Server). Using a proxy would result in Client -> Proxy -> Server. Now the proxy is located on your server. That means every user that connects to some server would use your ip which is not good. Ther