跳到主要内容

egret.WebSocket

   egret

   public class WebSocket

继承    egret.WebSocket → egret.EventDispatcheregret.HashObject

egret.WebSocket 类启用代码以建立传输控制协议 (TCP) 套接字连接,用于发送和接收字符串或二进制数据。要使用 egret.WebSocket 类的方法,请先使用构造函数 new egret.WebSocket 创建一个 egret.WebSocket 对象。套接字以异步方式传输和接收数据。

公共属性

属性
TYPE_BINARY : string
[静态]以二进制格式发送和接收数据
TYPE_STRING : string
[静态]以字符串格式发送和接收数据
connected : boolean
表示此 Socket 对象目前是否已连接
type : string
发送和接收数据的格式,默认是字符串格式

公共方法

方法
WebSocket(host:string,port:number)
创建一个 egret.WebSocket 对象参数为预留参数,现版本暂不处理,连接地址和端口号在 connect 函数中传入
close():void
关闭套接字
connect(host:string,port:number):void
将套接字连接到指定的主机和端口
connectByUrl(url:string):void
根据提供的url连接
flush():void
对套接字输出缓冲区中积累的所有数据进行刷新
readBytes(bytes:egret.ByteArray,offset:number,length:number):void
从套接字读取 length 参数指定的数据字节数
readUTF():string
从套接字读取一个 UTF-8 字符串
writeBytes(bytes:egret.ByteArray,offset:number,length:number):void
从指定的字节数组写入一系列字节
writeUTF(message:string):void
将字符串数据写入套接字

事件

Events
egret.Event.CONNECT
连接服务器成功。
egret.ProgressEvent.SOCKET_DATA
接收服务器数据。
egret.Event.CLOSE
在服务器关闭连接时调度。
egret.IOErrorEvent.IO_ERROR
在出现输入/输出错误并导致发送或加载操作失败时调度。。

属性详细信息

TYPE_BINARY

TYPE_BINARY : string = "webSocketTypeBinary"

  • 支持版本:Egret 2.4
  • 运行平台:Web,Native

以二进制格式发送和接收数据

TYPE_STRING

TYPE_STRING : string = "webSocketTypeString"

  • 支持版本:Egret 2.4
  • 运行平台:Web,Native

以字符串格式发送和接收数据

connected

connected : boolean

  • 支持版本:Egret 2.4
  • 运行平台:Web,Native

表示此 Socket 对象目前是否已连接

type

type : string

  • 支持版本:Egret 2.4
  • 运行平台:Web,Native

发送和接收数据的格式,默认是字符串格式

方法详细信息

WebSocket()

public WebSocket(host:string,port:number)

创建一个 egret.WebSocket 对象参数为预留参数,现版本暂不处理,连接地址和端口号在 connect 函数中传入

  • 支持版本:Egret 2.4
  • 运行平台:Web,Native
  • 参数

close()

public close():void

关闭套接字

  • 支持版本:Egret 2.4
  • 运行平台:Web,Native

connect()

public connect(host:string,port:number):void

将套接字连接到指定的主机和端口

  • 支持版本:Egret 2.4
  • 运行平台:Web,Native
  • 参数
    • host:string - 要连接到的主机的名称或 IP 地址
    • port:number - 要连接到的端口号

connectByUrl()

public connectByUrl(url:string):void

根据提供的url连接

  • 支持版本:all
  • 运行平台:Web,Native
  • 参数
    • url:string - 全地址。如ws://echo.websocket.org:80

flush()

public flush():void

对套接字输出缓冲区中积累的所有数据进行刷新

  • 支持版本:Egret 2.4
  • 运行平台:Web,Native

readBytes()

public readBytes(bytes:egret.ByteArray,offset:number,length:number):void

从套接字读取 length 参数指定的数据字节数。从 offset 所表示的位置开始,将这些字节读入指定的字节数组

  • 支持版本:Egret 2.4
  • 运行平台:Web,Native
  • 参数
    • bytes:egret.ByteArray - 要将数据读入的 ByteArray 对象
    • offset:number - 数据读取的偏移量应从该字节数组中开始
    • length:number - 要读取的字节数。默认值 0 导致读取所有可用的数据

readUTF()

public readUTF():string

从套接字读取一个 UTF-8 字符串

  • 支持版本:Egret 2.4
  • 运行平台:Web,Native

writeBytes()

public writeBytes(bytes:egret.ByteArray,offset:number,length:number):void

从指定的字节数组写入一系列字节。写入操作从 offset 指定的位置开始。如果省略了 length 参数,则默认长度 0 将导致该方法从 offset 开始写入整个缓冲区。如果还省略了 offset 参数,则写入整个缓冲区。

  • 支持版本:Egret 2.4
  • 运行平台:Web,Native
  • 参数
    • bytes:egret.ByteArray - 要从中读取数据的 ByteArray 对象
    • offset:number - ByteArray 对象中从零开始的偏移量,应由此开始执行数据写入
    • length:number - 要写入的字节数。默认值 0 导致从 offset 参数指定的值开始写入整个缓冲区

writeUTF()

public writeUTF(message:string):void

将字符串数据写入套接字

  • 支持版本:Egret 2.4
  • 运行平台:Web,Native
  • 参数
    • message:string - 要写入套接字的字符串

示例

/*
* 下面的示例使用 WebSocketExample 类创建新 WebSocket 对象,然后与服务器通讯。
*/
class WebSocketExample extends egret.DisplayObjectContainer {
public constructor() {
super();
this.initStateText();
this.initWebSocket();
}
private stateText: egret.TextField;
private text: string = "TestWebSocket";
private initStateText(): void {
this.stateText = new egret.TextField();
this.stateText.size = 22;
this.stateText.text = this.text;
this.stateText.width = 480;
this.addChild(this.stateText);
}
private socket: egret.WebSocket;
private initWebSocket(): void {
//创建 WebSocket 对象
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 {
//创建 ByteArray 对象
const byte: egret.ByteArray = new egret.ByteArray();
//写入字符串信息
byte.writeUTF("Hello Egret WebSocket");
//写入布尔值信息
byte.writeBoolean(false);
//写入int值信息
byte.writeInt(123);
byte.position = 0;
//发送数据
this.socket.writeBytes(byte, 0, byte.bytesAvailable);
}
private onSocketOpen(): void {
this.trace("WebSocketOpen");
this.sendData();
}
private onSocketClose(): void {
this.trace("WebSocketClose");
}
private onSocketError(): void {
this.trace("WebSocketError");
}
private onReceiveMessage(e: egret.Event): void {
//创建 ByteArray 对象
const byte: egret.ByteArray = new egret.ByteArray();
//读取数据
this.socket.readBytes(byte);
//读取字符串信息
const msg: string = byte.readUTF();
//读取布尔值信息
const boo: boolean = byte.readBoolean();
//读取int值信息
const num: number = byte.readInt();
this.trace("收到数据:");
this.trace("readUTF : " + msg);
this.trace("readBoolean : " + boo.toString());
this.trace("readInt : " + num.toString());
}
private trace(msg: any): void {
this.text = this.text + "\n" + msg;
this.stateText.text = this.text;
egret.log(msg);
}
}