Skip to content

Instantly share code, notes, and snippets.

@sunny352
Created February 10, 2019 13:50
Show Gist options
  • Save sunny352/8aa5569fb12a784c8666577716f56d94 to your computer and use it in GitHub Desktop.
Save sunny352/8aa5569fb12a784c8666577716f56d94 to your computer and use it in GitHub Desktop.
LayaAir的websocket连接示例
export default class WSNet {
//单例,方便使用
public static Inst(): WSNet {
if (null == WSNet._inst) {
WSNet._inst = new WSNet();
}
return WSNet._inst;
}
private static _inst: WSNet;
//websocket连接对象
private socket: Laya.Socket;
//构造函数
constructor() {
this.socket = new Laya.Socket();
//添加收到消息响应
this.socket.on(Laya.Event.MESSAGE, this, this.onReceive);
//添加连接成功响应
this.socket.on(Laya.Event.OPEN, this, this.onConnected);
//添加断开连接响应
this.socket.on(Laya.Event.CLOSE, this, this.onDisconnected);
//添加网络IO错误响应
this.socket.on(Laya.Event.ERROR, this, this.onError);
}
//收到消息的回调
private receiveCallback: Function;
//收到消息的回调对象
private receiveThisObject: any;
//设置收到消息的回调
public setReceiveCallback(callback: Function, thisObject: any): void {
this.receiveCallback = callback;
this.receiveThisObject = thisObject;
}
//连接到服务器,这里使用url方式进行连接,即标准的websocket连接方式
public connect(url: string): void {
this.socket.connectByUrl(url);
}
//断开连接
public disconnect(): void {
this.socket.close();
}
//发送消息到服务器,这里会序列化为json
public send(obj: any): void {
this.socket.send(JSON.stringify(obj))
}
//连接成功
private onConnected(): void {
console.log("on connect");
}
//断开连接
private onDisconnected(): void {
console.log("on disconnect")
}
//收到消息,这里会将收到的消息当作json文本并转换为对象提交给之前设置的收到消息的回调中去
private onReceive(message:any): void {
var jsonStr = message
if (null == this.receiveCallback) {
return;
}
var jsonObj = JSON.parse(jsonStr);
this.receiveCallback.call(this.receiveThisObject, jsonObj);
}
//错误处理
private onError(): void {
console.log("on error")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment