[]socket通讯返回数据
下面是复制的代码,标“*”处为有问题。
class Game {
private socket: Laya.Socket;
private byte: Laya.Byte;
constructor() {
//初始化引擎
Laya.init(600, 400, Laya.WebGL);
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);
// this.socket.send("hello world");//######################################################
}
private openHandler(event: any = null): void {
//正确建立连接;
console.log("建立连接");//***************************************************************************不写出
console.log(event);
}
private receiveHandler(msg: any = null): void {
///接收到数据触发函数
console.log("接收");//***************************************************************************不写出
console.log(msg);
}
private closeHandler(e: any = null): void {
//关闭事件
console.log("关闭事件");//**********************************************************************服务器 发数据时报错
console.log(e);
}
private errorHandler(e: any = null): void {
//连接出错
console.log("连接出错");//**********************************************************************服务器 发数据时报错
console.log(e);
}
}
new Game();
//-------------------------------------------------------------------------------------------------
当服务端发送数据时 控制台写出 报错如下:
连接出错
Event {isTrusted: true, type: "error", target: WebSocket, currentTarget: WebSocket, eventPhase: 2…}
关闭事件
CloseEvent {wasClean: false, code: 1006, reason: "", type: "close", target: WebSocket…}
//--------------------------------------------------------------------------------------------------
如果 运行 标“#”代码 this.socket.send("hello world");
控制台写出 报错如下如下:
Warning!,this class[MiniAdpter] already exist: Object {}
Error: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
at Socket.__proto.send (file:///K:/laya/mylaya12/bin/libs/laya.core.js:13509:16)
at new Game (file:///K:/laya/mylaya12/bin/js/LayaSample.js:17:21)
at file:///K:/laya/mylaya12/bin/js/LayaSample.js:45:1
class Game {
private socket: Laya.Socket;
private byte: Laya.Byte;
constructor() {
//初始化引擎
Laya.init(600, 400, Laya.WebGL);
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);
// this.socket.send("hello world");//######################################################
}
private openHandler(event: any = null): void {
//正确建立连接;
console.log("建立连接");//***************************************************************************不写出
console.log(event);
}
private receiveHandler(msg: any = null): void {
///接收到数据触发函数
console.log("接收");//***************************************************************************不写出
console.log(msg);
}
private closeHandler(e: any = null): void {
//关闭事件
console.log("关闭事件");//**********************************************************************服务器 发数据时报错
console.log(e);
}
private errorHandler(e: any = null): void {
//连接出错
console.log("连接出错");//**********************************************************************服务器 发数据时报错
console.log(e);
}
}
new Game();
//-------------------------------------------------------------------------------------------------
当服务端发送数据时 控制台写出 报错如下:
连接出错
Event {isTrusted: true, type: "error", target: WebSocket, currentTarget: WebSocket, eventPhase: 2…}
关闭事件
CloseEvent {wasClean: false, code: 1006, reason: "", type: "close", target: WebSocket…}
//--------------------------------------------------------------------------------------------------
如果 运行 标“#”代码 this.socket.send("hello world");
控制台写出 报错如下如下:
Warning!,this class[MiniAdpter] already exist: Object {}
Error: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
at Socket.__proto.send (file:///K:/laya/mylaya12/bin/libs/laya.core.js:13509:16)
at new Game (file:///K:/laya/mylaya12/bin/js/LayaSample.js:17:21)
at file:///K:/laya/mylaya12/bin/js/LayaSample.js:45:1
没有找到相关结果
已邀请:
要回复问题请先登录
5 个回复
Aar0n
赞同来自:
zxc1315
赞同来自:
Aar0n
赞同来自:
Aar0n
赞同来自:
Aar0n
赞同来自: