Main.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
class Main extends eui.UILayer { public constructor() { super(); this.once(egret.Event.ADDED_TO_STAGE, this.addToStage, this); } private addToStage() { Game.init(this.stage); egret.startTick(this.tickLoop, this); } tickLoop(timeStamp:number):boolean{ return false; } } class Game{ static I : Game; static mainStage: egret.Stage; static height: number; static width: number; static init(stage:egret.Stage) { Game.I = this; Game.height = egret.MainContext.instance.stage.stageHeight; Game.width = egret.MainContext.instance.stage.stageWidth; Game.mainStage = stage; /* メソッドなどを記入*/ new GameStage(); new Network(); } } class GameStage extends egret.DisplayObjectContainer{ static display : egret.DisplayObjectContainer = null; constructor(){ super(); this.initial(); } initial(){ GameStage.display = new egret.DisplayObjectContainer(); Game.mainStage.addChild(GameStage.display); } } class GameObject extends egret.DisplayObjectContainer{ object: egret.DisplayObjectContainer = null; static objects: GameObject[] = []; constructor(){ super(); this.initial(); } initial(){ this.object = new egret.DisplayObjectContainer(); GameStage.display.addChild(this.object); GameObject.objects.push(this); } } class Network extends egret.DisplayObjectContainer{ public constructor() { super(); this.initWebSocket(); } private socket:egret.WebSocket; private initWebSocket():void { this.socket = new egret.WebSocket(); this.socket.type = egret.WebSocket.TYPE_BINARY; this.socket.addEventListener(egret.ProgressEvent.SOCKET_DATA, this.onReceiveMessage, this); this.socket.addEventListener(egret.Event.CONNECT, this.onSocketOpen, this); this.socket.addEventListener(egret.Event.CLOSE, this.onSocketClose, this); this.socket.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onSocketError, this); this.socket.connect("echo.websocket.org", 80); } private sendData():void { let test:egret.ByteArray = new egret.ByteArray(); let object = { "number" : 1, "string" : "js-primer", "boolean" : true, "null" : null, "array" : [1, 2, 3] }; let jObject : string = JSON.stringify(object); test.writeUTF(jObject); this.socket.writeBytes(test, 0, test.bytesAvailable); } private onSocketOpen():void { console.log("WebSocketOpen"); this.sendData(); } private onSocketClose():void { console.log("WebSocketClose"); } private onSocketError():void { console.log("WebSocketError"); } private onReceiveMessage(e:egret.Event):void { let test:egret.ByteArray = new egret.ByteArray(); this.socket.readBytes(test); let jObject:string = test.readUTF(); let object = JSON.parse(jObject); console.log(object); console.log(object['number']); } } |
WebSocketのインスタンス化
1 2 3 4 5 6 7 8 9 10 11 |
private socket:egret.WebSocket; private initWebSocket():void { this.socket = new egret.WebSocket(); this.socket.type = egret.WebSocket.TYPE_BINARY; this.socket.addEventListener(egret.ProgressEvent.SOCKET_DATA, this.onReceiveMessage, this); this.socket.addEventListener(egret.Event.CONNECT, this.onSocketOpen, this); this.socket.addEventListener(egret.Event.CLOSE, this.onSocketClose, this); this.socket.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onSocketError, this); this.socket.connect("echo.websocket.org", 80); } |
socket.typeでbinary型かstring型を選択できます。
addEventListener(egret.ProgressEvent.SOCKET_DATA)でサーバーのデータを受信します。
socket.connect(“echo.websocket.org”, 80);はWebSocketのテストサーバーです。
データの送信
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
private sendData():void { let test:egret.ByteArray = new egret.ByteArray(); let object = { "number" : 1, "string" : "js-primer", "boolean" : true, "null" : null, "array" : [1, 2, 3] }; let jObject : string = JSON.stringify(object); test.writeUTF(jObject); this.socket.writeBytes(test, 0, test.bytesAvailable); } |
データの受信
1 2 3 4 5 6 7 8 9 |
private onReceiveMessage(e:egret.Event):void { let test:egret.ByteArray = new egret.ByteArray(); this.socket.readBytes(test); let jObject:string = test.readUTF(); let object = JSON.parse(jObject); console.log(object); console.log(object['number']); } |
参考
Egret Developer WebSocket
Egret Developer API WebSocket
Egret Developer API ByteArray