未找到 Haxe 类型:客户端

Haxe Type not found : Client

您好,我正在尝试在 Haxe 中创建一个 ThreadServer。我喜欢这种语言,几天前才开始接触它,它是 C# 和 AS3 的混合体,我都喜欢!

所以问题是当我尝试将客户端存储在一个列表中以便稍后访问它们时我需要它,但它告诉我找不到类型但它在同一个包中它应该能够访问它这是带有文件名和错误的代码。

错误:

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);
    }
}

感谢您的帮助!

因为 Client 是在名为 Server.hx 的文件(模块)中定义的,所以您需要在该文件外将其作为 Server.Client 进行寻址:

var client:Server.Client;

或者,将其单独移动到文件 Client.hx

有关此内容的更多信息,请参见 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.