import { Laya } from "Laya" ; import { Event } from "laya/events/Event" ; import { Socket } from "laya/net/Socket" ; import { Byte } from "laya/utils/Byte" ; export class Network_Socket2 { private socket : Socket ; private output : Byte ; constructor ( ) { Laya . init ( 550 , 400 ) ; this . connect ( ) ; } private connect ( ) { this . socket = new Socket ( ) ; this . socket . connectByUrl ( "wss://devxq.layabox.com:17000" ) ; this . output = this . socket . output ; this . socket . on ( Event . OPEN , this , this . onSocketOpen ) ; this . socket . on ( Event . CLOSE , this , this . onSocketClose ) ; this . socket . on ( Event . MESSAGE , this , this . onMessageReveived ) ; this . socket . on ( Event . ERROR , this , this . onConnectError ) ; } private onSocketOpen ( e = null ) { console . log ( "Connected" ) ; this . socket . send ( "demonstrate <sendString>" ) ; var message = "demonstrate <output.writeByte>" ; for ( var i = 0 ; i < message . length ; ++ i ) { this . output . writeByte ( message . charCodeAt ( i ) ) ; } this . socket . flush ( ) ; } private onSocketClose ( e = null ) { console . log ( "Socket closed" ) ; } private onMessageReveived ( message = null ) { console . log ( "Message from server:" ) ; if ( typeof ( message ) == 'string' ) { console . log ( message ) ; } else if ( message instanceof ArrayBuffer ) { console . log ( new Byte ( message ) . readUTFBytes ( ) ) ; } this . socket . input . clear ( ) ; } private onConnectError ( e = null ) { console . log ( "error" ) ; } }