When using flash.net.Socket class to connect to remote server, I encountered a strange situation. When writing socket directly in Flash Professional layer's code space, persistent connection can be established, and be established for only ONCE. However, if I wrapped that connection logic into a class, the class instance is constantly creating NEW sockets like crazy.
So here is my code when running socket standalone:
var host:String = 'xx.xx.xx.xx';
var port:Number = 123456;
trace("host:"+host);
trace("port:"+port);
if (true){
var socket:Socket = new Socket(host, port);
socket.addEventListener(Event.CONNECT, onConnect);
socket.addEventListener(Event.CLOSE, onClose);
socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
socket.addEventListener(ProgressEvent.SOCKET_DATA, onResponse);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError);
function onConnect(e:Event):void {
trace("Connected to socket...");
Log.info("Connected to socket...");
}
function onClose(e:Event):void {
// Security error is thrown if this line is excluded
trace("Closing socket...");
Log.info("Closing socket...");
socket.close();
}
function onError(e:IOErrorEvent):void {
trace("IO Error: "+e);
Log.info("IO Error: "+e);
}
function onSecError(e:SecurityErrorEvent):void {
trace("Security Error: "+e);
Log.info("Security Error: "+e);
}
function onResponse(e:ProgressEvent):void {
if (socket.bytesAvailable>0) {
var res:String = socket.readUTFBytes(socket.bytesAvailable);
trace(res);
Log.info(res);
}
}
}
And here is the code with socket in a class:
package com.example {
import flash.net.Socket;
import flash.events.*;
import com.example.utils.Log;
public class MockSession {
private var socket:Socket;
public function MockSession(host:String,port:Number):void{
socket = new Socket(host,port);
socket.addEventListener(Event.CONNECT, onConnect);
socket.addEventListener(Event.CLOSE, onClose);
socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
socket.addEventListener(ProgressEvent.SOCKET_DATA, onResponse);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError);
}
private function onConnect(e:Event):void {
trace("Connected to socket...");
Log.info("Connected to socket...");
}
private function onClose(e:Event):void {
// Security error is thrown if this line is excluded
trace("Closing socket...");
Log.info("Closing socket...");
socket.close();
}
private function onError(e:IOErrorEvent):void {
trace("IO Error: "+e);
Log.info("IO Error: "+e);
}
private function onSecError(e:SecurityErrorEvent):void {
trace("Security Error: "+e);
Log.info("Security Error: "+e);
}
private function onResponse(e:ProgressEvent):void {
if (socket.bytesAvailable>0) {
var res:String = socket.readUTFBytes(socket.bytesAvailable);
trace(res);
Log.info(res);
}
}
}
}
And how it is run in Flash code layer:
import com.example.MockSession;
var session2:MockSession = new MockSession('xx.xx.xx.xx', 123456);
This is producing a bunch of "socket connected" msg...
Connected to socket...
Connected to socket...
Connected to socket...
Connected to socket...
Connected to socket...
Connected to socket...
Connected to socket...
Connected to socket...
......
I can't figure out how using class could be in any difference. Can anyone help me?