Reptic
Reptic

Reputation: 175

Haxe Type not found : Client

Hi am trying to create a ThreadServer in Haxe. I love this language, just got into it couple days ago it's a mix between C# and AS3 which I love both!

So the problem is when I try to store the Clients in a List to access them later example to kick etc I need it but it tells me Type not found but it's in the same package it should be able to access it here is the code with the file names and the error.

Error:

var cl:Client = { id: num, cSocket:s };
var cData = new ClientData(cl);
Main.clients.add(cData);

Server.hx:

package;

import neko.Lib;
import sys.net.Socket;
import neko.net.ThreadServer;
import haxe.io.Bytes;

typedef Client =
{
    var id : Int;
    var cSocket:Socket;
}

typedef Message =
{
    var str : String;
}

class Server extends ThreadServer<Client, Message>
{
    // create a Client
    override function clientConnected( s : Socket ) : Client
    {
        var num = Std.random(100);
        Lib.println("client " + num + " is " + s.peer());

        var cl:Client = { id: num, cSocket:s };
        var cData = new ClientData(cl);
        Main.clients.add(cData);

        return cl;
    }

    override function clientDisconnected( c : Client )
    {
        Lib.println("client " + Std.string(c.id) + " disconnected");
    }

    override function readClientMessage(c:Client, buf:Bytes, pos:Int, len:Int)
    {
        var complete = false;
        var cpos = pos;
        while (cpos < (pos+len) && !complete)
        {
            complete = (buf.get(cpos) == 0);
            cpos++;
        }

        if ( !complete ) return null;

        var msg:String = buf.getString(pos, cpos-pos);
        return {msg: {str: msg}, bytes: cpos-pos};
    }

    override function clientMessage( c : Client, msg : Message )
    {
        Lib.println(c.id + " got: " + msg.str);
    }
}

ClientData.hx:

package;

class ClientData
{
    var client:Client;

    public function new(c:Client)
    {
        this.client = c;
    }
}

Main.hx:

package;

class Main 
{
    public static var clients=new List<ClientData>();

    static function main() 
    {
      var server = new Server();
      server.run("localhost", 5588);
    }
}

Thanks for the help!

Upvotes: 1

Views: 834

Answers (1)

Thomas
Thomas

Reputation: 181715

Because Client is defined in a file (module) named Server.hx, you need to address it as Server.Client outside that file:

var client:Server.Client;

Alternatively, move it to a file Client.hx all by itself.

More about this in the manual:

The distinction of a module and its containing type of the same name is blurry by design. In fact, addressing haxe.ds.StringMap can be considered shorthand for haxe.ds.StringMap.StringMap.

...

If the module and type name are equal, the duplicate can be removed, leading to the haxe.ds.StringMap short version. However, knowing about the extended version helps with understanding how module sub-types are addressed.

Upvotes: 4

Related Questions