Skip to main content
Participant
July 16, 2007
Answered

FMS auto disconnects when using a class

  • July 16, 2007
  • 1 reply
  • 306 views
I am trying to implement a very simple class that will create an instance of the NetConnection object and connect to the FMS server. However, the client will connect then immediately disconnect when the swf is published. My code is as follows...

FLA Code:
import _includes.*;
var obj_conn:Conn = new Conn();
obj_conn.connect();

Conn.as Code:
class _includes.Conn
{
public function Conn()
{
}

public function connect()
{
var nc:NetConnection = new NetConnection();
nc.connect("rtmp://localhost/classtest");
}
}


Please note that the connection works fine if I just create a NetConnection variable on the FLA itself in the timeline. Any suggestions/help would be appriciated!
    This topic has been closed for replies.
    Correct answer
    it think this is happen because you use local variable : your variable is delete at the end of connect() so your NetConnection will be closed in the same time.

    try this:

    Conn.as Code:
    class _includes.Conn
    {

    private var objNetCon:NetConnection;

    public function Conn()
    {
    }

    public function connect()
    {
    this.objNetCon = new NetConnection();
    this.objNetCon.connect("rtmp://localhost/classtest");
    }
    }

    1 reply

    Correct answer
    July 16, 2007
    it think this is happen because you use local variable : your variable is delete at the end of connect() so your NetConnection will be closed in the same time.

    try this:

    Conn.as Code:
    class _includes.Conn
    {

    private var objNetCon:NetConnection;

    public function Conn()
    {
    }

    public function connect()
    {
    this.objNetCon = new NetConnection();
    this.objNetCon.connect("rtmp://localhost/classtest");
    }
    }
    Participant
    July 16, 2007
    It looks like that works.... thank you!