[]socket.sent

下面是复制的源码 openHandler 和 receiveHandler 没有响应
 class Game {
private socket: Laya.Socket;
private byte: Laya.Byte;
constructor() {



//初始化引擎
//Laya.init(600, 400, Laya.WebGL);
Laya.init(600,400);
Laya.stage.scaleMode = Laya.Stage.SCALE_SHOWALL;
Laya.stage.screenMode = Laya.Stage.SCREEN_NONE;
var uiResArr =[
{url:"res/atlas/comp.atlas",type: Laya.Loader.ATLAS},
];
Laya.loader.load (uiResArr , Laya.Handler.create(this,this.onLoded));
     //加载图片


this.byte = new Laya.Byte();
//这里我们采用小端
this.byte.endian = Laya.Byte.LITTLE_ENDIAN;
this.socket = new Laya.Socket();
//这里我们采用小端
this.socket.endian = Laya.Byte.LITTLE_ENDIAN;
//建立连接
this.socket.connectByUrl("ws://localhost:8899");
this.socket.on(Laya.Event.OPEN, this, this.openHandler);
this.socket.on(Laya.Event.MESSAGE, this, this.receiveHandler);
this.socket.on(Laya.Event.CLOSE, this, this.closeHandler);
this.socket.on(Laya.Event.ERROR, this, this.errorHandler);


}
private openHandler(event: any = null): void {
//正确建立连接;
console.log("建立"+event);//没有写出
}
private receiveHandler(msg: any = null): void {
///接收到数据触发函数
console.log("收到"+msg);;//没有写出
console.log(msg);;//没有写出
}
private closeHandler(e: any = null): void {
//关闭事件
console.log("关闭"+e);
}
private errorHandler(e: any = null): void {
//连接出错
console.log("错误"+e);
console.log(e);
}
onLoded():void{
// Laya.stage.addChild(obj);
Laya.stage.addChild(new Btn1 ());//加入舞台
}

}
new Game();
 
 
 

class Btn1 extends ui.v1UI{
public socket: Laya.Socket;
public byte: Laya.Byte;
constructor(){
super();
this.btn1.on(Laya.Event.CLICK,this,this.Btn1On);
}
Btn1On():void{
// socket.connect(“192.168.0.1.133”,8899);
console.log("kjk");
this.socket.send("hello world");//这是发送字符串的形式。
//报错TypeError: Cannot read property 'send' of undefined
//    at Btn1.Btn1On (file:///K:/laya/mylaya11/bin/js/Btn1.js:21:20)
//    at EventHandler.__proto.runWith (file:///K:/laya/mylaya11/bin/libs/laya.core.js:711:59)
//    at Button.__proto.event (file:///K:/laya/mylaya11/bin/libs/laya.core.js:488:30)
//    at TouchManager.__proto.sendEvents (file:///K:/laya/mylaya11/bin/libs/laya.core.js:3265:7)
}

}
已邀请:

leisure

赞同来自:

 socket: Laya.Socket;
这还没有实例化呢,注重下基础和细心点。

要回复问题请先

商务合作
商务合作