2017-08-17 112 views
1

您好,我正在尝试在Haxe中创建一个ThreadServer。我喜欢这种语言,几天前刚刚进入它,它是C#和AS3的混合体,我喜欢它们!Haxe类型未找到:客户端

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

错误:

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

感谢您的帮助!

回答

4

因为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.

+0

OMG是如此愚蠢的SRS感谢这个答案只是做了它移动的类型Client.hx和它的工作!真棒,刚刚学到了新东西:) – Reptic

+0

你不傻; Haxe语言在这里违反了[至少令人惊讶的原则](https://en.wikipedia.org/wiki/Principle_of_least_astonishment)(正如往常一样)。附:如果答案适合你,请[接受](https://stackoverflow.com/help/someone-answers)。 – Thomas

+0

我会接受它只是需要等待几分钟才能做到这一点,只是一个快速的问题是ThreadServer可扩展的大项目等? – Reptic