import { Laya } from "Laya" ; import { Socket } from "laya/net/Socket" ; import { Utils } from "laya/utils/Utils" ; export default class Client { static _instance : Client = null ; socket : Socket ; constructor ( ) { } static get instance ( ) : Client { if ( ! Client . _instance ) { Client . _instance = new Client ( ) ; Client . _instance . initEvent ( ) ; } return Client . _instance ; } static init ( ) { } initEvent ( ) { var host = "10.10.20.80" ; var post = 10000 ; var websocketurl = "ws://" + host + ":" + post + "/" ; this . socket = new Socket ( ) ; this . socket . on ( "open" , this , this . onScoektOpen ) ; this . socket . on ( "message" , this , this . onSocketMssage ) ; this . socket . on ( "close" , this , this . onSocketClose ) ; this . socket . on ( "error" , this , this . onSocketClose ) ; this . socket . connectByUrl ( websocketurl ) ; } param data { "type" send ( data ) { var isMaster = parseInt ( Utils . getQueryString ( "isMaster" ) ) || 0 ; if ( ! isMaster ) return ; if ( this . socket ) { data . isMaster = isMaster ; this . socket . send ( JSON . stringify ( data ) ) ; } } onScoektOpen ( ) { console . log ( "websocket open" ) ; } onSocketMssage ( data ) { console . log ( "接收信息 data：" + data ) ; var data = JSON . parse ( data ) ; Laya . stage . event ( data . type , [ data ] ) ; } onSocketClose ( ) { console . log ( "websocket close" ) ; } }