參考
關(guān)于Big Endian 和 Little Endian
Laya官方示例 Byte 二進(jìn)制讀寫
js筆記一 ArrayBuffer TypeArray
在開發(fā)項(xiàng)目中,二進(jìn)制的操作是不可或缺的颈畸。在html5時(shí)代,對(duì)二進(jìn)制的支持已經(jīng)有了很大的突破礁苗。但是api的繁瑣徙缴,對(duì)開發(fā)者開發(fā)項(xiàng)目來說不太方便嘁信。在頁(yè)游時(shí)代潘靖,Actionscript3.0的二進(jìn)制數(shù)組ByteArray蚤蔓,功能完善,api操作簡(jiǎn)單易懂单寂,因此Laya的Byte在參考ByteArray的同時(shí)承接了html5的TypedArray類型化數(shù)組的特點(diǎn)吐辙。
以下代碼參考網(wǎng)絡(luò)和格式--Socket
module laya {
import Event = Laya.Event;
import Socket = Laya.Socket;
import Byte = Laya.Byte;
export class NetWork_Socket {
private socket: Socket;
private output: Byte;
constructor() {
Laya.init(550, 400);
this.connect();
}
private connect(): void {
this.socket = new Socket();
//this.socket.connect("echo.websocket.org", 80);
this.socket.connectByUrl("ws://echo.websocket.org:80");
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(): void {
console.log("Connected");
// 發(fā)送字符串
this.socket.send("demonstrate <sendString>");
// 使用output.writeByte發(fā)送
var message: string = "demonstrate <output.writeByte>";
for (var i: number = 0; i < message.length; ++i) {
this.output.writeByte(message.charCodeAt(i));
}
this.socket.flush();
}
private onSocketClose(): void {
console.log("Socket closed");
}
private onMessageReveived(message: any): void {
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: Event): void {
console.log("error");
}
}
}
new laya.NetWork_Socket();
以下參考Laya官方示例 WebSocket發(fā)送與接收
注意:我們看到我們實(shí)例化Byte和socket的時(shí)候都設(shè)置了endian尊沸,這個(gè)是很容易忽略的地方贤惯,有些開發(fā)者不注意這個(gè),前端和服務(wù)端的endian不一致壶熏,導(dǎo)致了接收的數(shù)據(jù)是亂碼浦译,所以讀寫數(shù)據(jù)的時(shí)候一定要保證endian的一致。
var by:Byte = new Byte();//這里聲明一個(gè)臨時(shí)Byte類型
by.endian = Byte.LITTLE_ENDIAN;//設(shè)置endian帽哑;
by.writeInt32(5000);//寫入一個(gè)int32數(shù)據(jù)
by.writeUint16(16);//寫入一個(gè)uint16 數(shù)據(jù)
byte.writeArrayBuffer(by.buffer);//把臨時(shí)字節(jié)數(shù)據(jù)的數(shù)據(jù)寫入byte中妻枕,這里注意寫入的是by.buffer;
byte.clear();//清除掉數(shù)據(jù);方便下次讀寫粘驰;
this.socket.send(this.byte.buffer);//這里是把字節(jié)數(shù)組的數(shù)據(jù)通過socket發(fā)送給服務(wù)器。
上面我們看到愕掏,我們通過一個(gè)字節(jié)數(shù)組把我們需要的數(shù)據(jù)讀入一個(gè)Byte數(shù)組顶伞,最后發(fā)送給服務(wù)器的是byte.buffer,這是一個(gè)ArrayBuffer的數(shù)據(jù)類型剑梳。這里一定要注意send的參數(shù)是ArrayBuffer垢乙,很多開發(fā)者可能不注意,直接傳遞成了Byte侨赡,導(dǎo)致發(fā)送數(shù)據(jù)不正確粱侣。假如寫成this.socket.send(this.byte);這是錯(cuò)誤的齐婴,這點(diǎn)一定要注意稠茂。
private function receiveHandler(msg:Object = null):void
{
//.............這里我們假設(shè)收到的是二進(jìn)制ArrayBuffer
this.byte.clear();
this.byte.writeArrayBuffer(msg);//把接收到的二進(jìn)制數(shù)據(jù)讀進(jìn)byte數(shù)組便于解析。
this.byte.pos = 0;//設(shè)置偏移指針诱担;
////下面開始讀取數(shù)據(jù)电爹,按照服務(wù)器傳遞過來的數(shù)據(jù),按照順序讀取
var a:int = this.byte.getByte();
var b:int = this.byte.getInt16();
var c:Number = this.byte.getFloat32();
var d:String = this.byte.getString();
var e:String = this.byte.getUTFString();
}
判斷類型可以參考socket那個(gè)例子:
private onMessageReveived(message: any): void {
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();
}